gpt4 book ai didi

javascript - 累加器在 make `reduce` 方法中返回未定义

转载 作者:行者123 更新时间:2023-12-02 02:42:16 25 4
gpt4 key购买 nike

我尝试使函数与 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] 并在初始值(函数a)的第三个参数未传递。

最佳答案

Object.keys 始终返回一个字符串数组,因此 i 是一个字符串,因此 i === 0 始终为 false。

另请注意,您需要声明i。现在,您的代码正在成为 The Horror of Implicit Globals 的牺牲品。 (我建议使用严格模式,这样它们就应该是错误。)

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。请参阅the specification有关详细信息,但 reduce 检查它获得了多少个参数,它不会检查累加器是否未定义。如果您明确为累加器指定值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;
}

在归约过程中,累加器也可能变得未定义。如果没有给定累加器,则使用第一个看到的值(不一定位于稀疏数组中的索引 0 处!)。 (我对上面累加器的修复实际上修复了这个副产品。)您的代码还使用 Object.keysreduce 仅使用 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}));

关于javascript - 累加器在 make `reduce` 方法中返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63376645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com