var potatos = [
{'id':'1001',weight:50},
{'id':'1002',weight:60},
{'id':'1003',weight:70},
{'id':'1004',weight:80},
{'id':'1005',weight:90},
{'id':'1006',weight:100},
]
forEach
要求:每个weight加20
potatos.forEach(item=>item.weight+=20)
等价于
potatos.forEach(function(item){
return item.weight = item.weight + 20
})
console.log(potatos)
map
要求:每个weight加20
weight = potatos.map(item=>item.weight+=20)
等价于
weight = potatos.map(function(item){
return item.weight = item.weight + 20
})
console.log(potatos)
console.log(weight)
和forEach不同地方在于map会把weight的值返回出来
filter
要求:筛选出weight大于70的数据
filter = potatos.filter(item=> item.weight > 70)
console.log(filter )
some
要求:判断数据中是否有weight大于70的,如果找到,返回true,并且停止遍历
some = potatos.some(item=> item.weight > 70)
console.log(some)
every
要求:判断数据中是否每个weight都大于70,如果是,返回true
every= potatos.every(item=> item.weight > 70)
console.log(every)
find
要求:找到一个weight大于70的值
find= potatos.find(item=> item.weight > 70)
console.log(find)
findIndex
要求:找到一个weight大于70的值的索引
findIndex= potatos.findIndex(item=> item.weight > 70)
console.log(findIndex)
reduce
要求:累加所有的weight
totalWeight = potatos.reduce((sum,item)=>sum+item.weight,0)
等价于
var totalWeight = 0;
potatos.forEach(potato => {
return totalWeight += potato.weight;
});
console.log(totalWeight);