reduce() 方法对数组中的每个元素执行一个升序执行的 reducer 函数,并将结果汇总为单个返回值
const array1 = [1, 2, 3, 4];
const reducer = (accumulator...reduce 方法的参数
1、第一个参数:reducer 函数
其中,reducer 函数又有四个参数:
Accumulator (acc) (累计器)
Current Value (cur) (当前值...可以看到如果不传第二个参数 initialValue,则函数的第一次执行会将数组中的第一个元素作为 total 参数返回。...如果传了第二个参数 initialValue,那么第一次执行的时候 total 的值就是传递的参数值,然后再依次遍历数组中的元素。...reduce(
function(a, b) {
return a.concat(b);
},
[]
);
// flattened is [0, 1, 2, 3, 4, 5]
4、计算数组中每个元素出现的次数