数组的map()方法

83 阅读1分钟

array.map()

  //功能1:同forEach()方法
  
  const arr3 = ['苹果','橘子','西瓜']
  arr3.map((value,index,self)=>{
    console.log('arr3.map()--value',value)
    console.log('arr3.map()--index',index)
    console.log('arr3.map()--self',self)
  })


  //功能2:每次回调函数的返回值被map组成新数组返回
  var b = arr3.map((value,index,self)=>{
    return { 
      'name':value,
      'id': index + 1,
    }
  })
  console.log('b',b) // [{id: 1,name: "苹果"},{id: 2,name: "橘子"},{id: 3,name: "西瓜"}]
  console.log(arr3);   //['苹果','橘子','西瓜']---原数组未改变