map()javascript
map()方法按照原始数组元素顺序依次处理元素,返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。可看做是对原数组进行映射。html
注意:map()方法不会对空数组进行检测。java
array.map(function(currentValue,index,arr), thisValue) //参数:当前元素、当前元素的索引、当前元素所属的数组 var wallets = people.map(function (dude) {
return dude.wallet;
});
forEach()web
forEach()方法用于遍历数组的每一个元素,将元素传给回调函数。数组
注意:forEach()对于空数组是不会调用回调函数的。 svg
array.forEach(function(currentValue, index, arr), thisValue) //参数:当前元素、当前元素的索引、当前元素所属的数组 people.forEach(function (dude) {
dude.pickUpSoap();
});
reduce()wordpress
reduce()方法是让数组中的前项和后项作某种计算,并累计最终值。函数
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)初始值 //参数:初始值(计算结束的返回值)、当前元素、当前元素的索引、当前元素所属的数组 var totalMoney = wallets.reduce(function (countedMoney, wallet) {
return countedMoney + wallet.money;
}, 0);
filter()this
filter()方法会筛选出数组中符合条件的项,组成一个新数组。spa
array.filter(function(currentValue,index,arr), thisValue) //参数:当前元素、当前元素的索引、当前元素所属的数组 var fatWallets = wallets.filter(function (wallet) {
return wallet.money > 100;
});
参考文章: