forEach,map,filter,some,find,findIndex,reduce用法

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值