首页 > 解决方案 > 累加器在制作`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的第三个参数)。

标签: javascriptreduce

解决方案


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.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}));


推荐阅读