累加器 returns 在 making `reduce` 方法中未定义

accumulator returns undefined within making `reduce` method

我试图让一个功能与 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在初始值(函数a的第三个参数)未传递时不设置为collection[i]并保持undefined

Object.keys总是returns一个字符串数组,所以i是一个字符串,所以i === 0总是假的.

另请注意,您需要声明 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。如果您明确地为累加器指定值 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!)。 (我对上述累加器的修复实际上将其修复为 by-product。)您的代码还使用 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}));