【问题标题】:accumulator returns undefined within making `reduce` method累加器在制作`reduce`方法中返回未定义
【发布时间】:2020-12-02 05:01:37
【问题描述】:

我试图让一个函数与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的第三个参数)未传递时保持为undefined

【问题讨论】:

    标签: javascript reduce


    【解决方案1】:

    Object.keys 总是返回一个字符串的数组,所以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 处!)。 (我对上面累加器的修复实际上是作为副产品修复的。)您的代码还使用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}));

    【讨论】:

    • 感谢教我一些我完全不理解甚至完全没有注意到的东西。喜欢你的回答
    猜你喜欢
    • 2021-11-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    相关资源
    最近更新 更多