javascript - 累加器在制作`reduce`方法中返回未定义
问题描述
我试图使函数与Array.prototype.reduce
方法相同。
function a(collection, iterator, accumulator) {
for (i of Object.keys(collection)){
if (i===0 && accumulator === undefined){
accumulator = collection[i]
continue
}
accumulator = iterator(accumulator, collection[i])
}
return accumulator;
};
a([1,2,3],function(acc,cur){return acc += cur}, 0) // this works fine. returns 6
a([1,2,3],function(acc,cur){return acc += cur}) // expected 6 but returns NaN
a([1,2,3], function(memo){return memo}); // expected 1 but returns undefined
我不知道为什么accumulator
不设置为collection[i]
并保持原样undefined
时未传递初始值(函数a的第三个参数)。
解决方案
Object.keys
总是返回一个字符串数组,所以i
是一个字符串,所以i === 0
总是假的。
另请注意,您需要声明i
. 现在,您的代码正在成为隐式全局恐怖的牺牲品。(我建议使用严格模式,这样他们就应该一直是错误的。)
FWIW,这是解决这两个问题的最低更新(但也见下文):
"use strict";
function a(collection, iterator, accumulator) {
let first = true; // ***
for (const i of Object.keys(collection)){
// ^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− *** declare `i`
if (first && accumulator === undefined){
// ^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− *** first pass?
first = false; // ***
accumulator = collection[i]
continue
}
first = false; // ***
accumulator = iterator(accumulator, collection[i])
}
return accumulator;
};
console.log(a([1,2,3],function(acc,cur){return acc += cur}, 0));
console.log(a([1,2,3],function(acc,cur){return acc += cur}));
console.log(a([1,2,3], function(memo){return memo}));
FWIW,我不知道您的意思是不是完全匹配reduce
,但如果是这样,您的函数逻辑与Array.prototype.reduce
. 有关详细信息,请参阅规范,但reduce
检查它有多少参数,它不检查累加器是否为undefined
. 如果您明确地给它undefined
累加器的值,它将使用该值(而不是使用第一个数组元素的值):
console.log("`undefined` for accumulator:");
["a", "b", "c"].reduce((acc, value) => { console.log(acc, value); return value; }, undefined);
console.log("No accumulator:");
["a", "b", "c"].reduce((acc, value) => { console.log(acc, value); return value; })
.as-console-wrapper {
max-height: 100% !important;
}
累加器也可以在还原期间变为。 undefined
如果没有给出累加器,则它是第一个看到的值(不一定在稀疏数组中的索引 0 处!)。(我对上面的累加器的修复实际上将其作为副产品修复。)您的代码也使用Object.keys
而reduce
只使用for
循环。我没有仔细阅读,可能还有其他差异。
"use strict";
function a(collection, iterator, ...optional) { // ***
let haveAccumulator = optional.length > 0; // *** Default acc correctly
let [accumulator] = optional; // ***
for (const i of Object.keys(collection)){
// ^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− *** declare `i`
if (!haveAccumulator) { // ***
haveAccumulator = true; // ***
accumulator = collection[i]
continue;
}
accumulator = iterator(accumulator, collection[i])
}
return accumulator;
};
console.log(a([1,2,3],function(acc,cur){return acc += cur}, 0));
console.log(a([1,2,3],function(acc,cur){return acc += cur}));
console.log(a([1,2,3], function(memo){return memo}));
推荐阅读
- java - 如何将 Hibernate 对象导出到插入语句中
- c# - 有没有办法通过 c# 中的名称访问变量的引用
- vector - 光流和运动矢量有什么区别?
- django - 如何在索引/主页中呈现我的联系表格
- reactjs - React use component in inline style
- javascript - Issue in Conversion of 6*6 array into 3*3 array in javascript
- android - gitpod.io 终端中没有键盘输入
- c - 此行的含义: (void)(pointer);
- ios - Xamarin 表单:如何从 ios 捆绑签名中清除旧的分发证书和配置文件?
- django - How to keep files in the formset after page reload? . Django