array.reduce(function(total, currentValue, currentIndex, array){
return ...;
}, initialValue)
total是每轮迭代的结果,currentValue是当前迭代所处理的元素, currentIndex是所处理元素的索引,array就是数组本身,initialValue是total的初始值,没有的话就默认数组的第一个元素。
例子,求数组的总和。
let array = [1,2,3,4,5];
let sum = array.reduce(function(total, currentValue){
return total+currentValue;
}, 0);
console.log(sum); // 15