本文共 726 字,大约阅读时间需要 2 分钟。
reduce()
is another important method of an array.
reduce()
是数组的另一个重要方法。
reduce()
executes a callback function on all the items of the array and allows to progressively compute a result. If initialValue
is specified, accumulator
in the first iteration will equal to that value.
reduce()
在数组的所有项目上执行回调函数,并允许逐步计算结果。 如果指定了initialValue
,则第一次迭代中的accumulator
将等于该值。
a.reduce((accumulator, currentValue, currentIndex, array) => { //...}, initialValue)
Example:
例:
[1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => { return accumulator * currentValue}, 1)// iteration 1: 1 * 1 => return 1// iteration 2: 1 * 2 => return 2// iteration 3: 2 * 3 => return 6// iteration 4: 6 * 4 => return 24// return value is 24
翻译自:
转载地址:http://gpqgb.baihongyu.com/