语法
array.reduce(function(prevValue, currentValue, currentIndex, arr), initialValue)
示例:累加计算
let list = [1, 2, 3, 5]; let result = list.reduce((prevValue, currentValue)=>{ return prevValue + currentValue; }, 0); console.log(result); // 11
示例:合并数组
let list = [[1, 2, 3], [], [8, 9]]; let result = list.reduce((prevArr, currentArr)=>{ return prevArr.concat(currentArr); }, []); console.log(result); // [ 1, 2, 3, 8, 9 ]
参考