为什么javascript的reduce实现会在第一次迭代时跳过执行?
[1,2,3].reduce((acc, val) => {
console.log('acc',acc);
console.log('val',val)
return acc + val;
});
// acc 1
// val 2
// acc 3
// val 3
// 6
我注意到第一个语句执行从未运行(在这种情况下,我预计会有 6 个控制台日志,每个元素 2 个)。当我尝试在每次迭代中使用 reduce 执行具有副作用的函数时,这是非常出乎意料的行为。
在我使用过的其他语言中,传递的列表的每次迭代都会执行。还有其他例子吗?
为什么会这样,为什么javascript原生的Array reduce的实现是这样的?
========================= 编辑 1/解决方案 ================== ======
为了确保它通过第一次迭代,给它一个初始值(这里的第二个参数/在这种情况下为 0)
[1,2,3].reduce((acc, val) => {
console.log('acc',acc);
console.log('val',val)
返回 acc + val;
}, 0);
最佳答案
这是因为在每次迭代中,第一个值都被视为返回
值(或累加器)。
直接来自 here , 你可以看到
The accumulator accumulates the callback's return values; it is the accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied (see below).
如果我们查看源代码here ,我们可以看到它是如何实现的:
Array.prototype.myReduce = function(callback, initialVal) {
var accumulator = (initialVal === undefined) ? undefined : initialVal;
for (var i = 0; i < this.length; i++) {
if (accumulator !== undefined)
accumulator = callback.call(undefined, accumulator, this[i], i, this);
else
accumulator = this[i];
}
return accumulator;
};
在else
结构中,我们可以看到,如果值为undefined
,我们将其设置为i-th
中的子索引大批;对于第一次迭代,这是第一个。之后,它成为后续迭代的回调(返回)值。
如果需要,您可以回溯并检查输出。
关于Javascript 减少陷阱 - 跳过第一次迭代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52245352/