search

Home  >  Q&A  >  body text

javascript - I would like to ask, what does i in _map(data,(item,i)=>{}) and _each(data,(item,i)=>{}) do?

Like the title, can you not write it

世界只因有你世界只因有你2825 days ago596

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 13:41:53

    i是遍历元素的索引。
    如原生的mapforEachMethod.

    ['a', 'b', 'c'].map(function(item, i, array){
        console.log(item, i, array);
    });
    ['a', 'b', 'c'].forEach(function(item, i, array){
        console.log(item, i, array);
    });

    item为当前项,即当前遍历的元素本身。分别为a, b, c
    i为元素处于数组中的下标或索引。分别为 0, 1, 2
    array为数组本身。值为['a', 'b', 'c']

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:41:53

    i is the index corresponding to the item in the data and can be omitted

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:41:53

    Refers to the native map, item is a reference to the data item, i represents the index. i can be omitted

    For example:

    var arr = [1,2,3];
    arr.map(function(item){
      if(item == 2){
        item = 100; // arr 是不会变成[1,100,3],因为 item 改变不影响原数组,它只是个引用
      }
    })

    If it is like the following, arr will be changed

    arr = arr.map(function(item){
      if(item == 2){
        item = 100; 
      }
      return item
    })
    

    reply
    0
  • Cancelreply