【study】forEach,map,filter之间的区别

119 阅读1分钟
 var potatos = [
  { id: '1001', weight: 50 },
  { id: '1002', weight: 80 },
  { id: '1003', weight: 120 },
  { id: '1004', weight: 40 },
  { id: '1005', weight: 110 },
  { id: '1006', weight: 60 }
]

// filter 
// 不改变原数组
// let a = potatos.filter((item)=>{
// 	return item.weight > 100
// })
// {id: "1003", weight: 120}
// {id: "1005", weight: 110}

// forEach
// 会改变原数组,没有返回值,返回值是undefined
// let a = potatos.forEach((item)=>{
// 	// console.log(item.weight)
// 	return item.weight += 20
// })
// undefined
   
// map
// 会改变原数组,有返回值,返回值可以return出来
// let a = potatos.map((item)=>{
// 	return item.weight += 1
// })
// [51, 81, 121, 41, 111, 61]