简体   繁体   English

为什么我的reduce函数中的累加器不采用回调的返回值?

[英]Why doesn't the accumulator in my reduce function take on the callback's return value?

I have an array of objects and I want to sum all the "bps" values in my object for the entire array. 我有一个对象数组,我想对整个数组的对象中的所有“ bps”值求和。

My array of objects looks like this: 我的对象数组如下所示:

arr = [
    {
        date: "2017-06-14T14:00:00.000Z",
        bps: 2
    },
    ...
]

Here is my reduce function: 这是我的reduce函数:

arr.reduce((accum, currVal) => {
        console.log(accum.bps);
        console.log(currVal.bps);
        console.log(accum.bps + currVal.bps);
        return accum.bps + currVal.bps;
    }, {
        bps: 0
    });

Based on what has been outputted to the console, it appears that after the first iteration of the reduce function, the return value of 0 does not become the next iteration's accumulator (it becomes "undefined"). 根据已输出到控制台的内容,在reduce函数的第一次迭代之后,返回值0不会成为下一个迭代的累加器(它变为“未定义”)。 Why is this the case and what should my function look like to sum up all the "bps" values in my array? 为什么会这样,我的函数应该如何总结数组中的所有“ bps”值?

The is what the console is displaying 控制台显示的是

scripts.js:1024 0
scripts.js:1025 0
scripts.js:1026 0
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 1.95
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1024 undefined
scripts.js:1025 0
scripts.js:1026 NaN
scripts.js:1031 NaN

As your reduce is simply adding all the .bps values together, you can simply map all the array items to an array of numbers, then add those together 由于reduce只是将所有.bps值加在一起,因此您可以简单地将所有数组项映射到一个数字数组,然后将它们加在一起

arr.map(({bps}) => bps)
.reduce((a, b) => a + b);

or even 甚至

arr.reduce((accum, {bps}) => accum + bps, 0);

You should return an object that has a bps key, not a pure sum. 您应该返回一个具有bps密钥而不是纯总和的对象。

Ie return { bps: accum.bps + currVal.bps }; return { bps: accum.bps + currVal.bps }; instead of return accum.bps + currVal.bps; 而不是return accum.bps + currVal.bps;


arr
  .reduce((accum, currVal) => {
    console.log(accum.bps);
    console.log(currVal.bps);
    console.log(accum.bps + currVal.bps);
    return { bps: accum.bps + currVal.bps };
  }, {
    bps: 0
  })
  .bps;

Alternatively (this will actually work faster, especially for long arrays, because it doesn't create unnecessary objects, only the result object). 或者 (这实际上会更快,特别是对于长数组,因为它不会创建不必要的对象,而只会创建结果对象)。

 
 
 
 
  
  
  const bpsResult = { bps: arr .reduce((accum, currVal) => { console.log(accum); console.log(currVal); console.log(accum + currVal); return accum + currVal; }, 0); };
 
 
  

This works. 这可行。 accum is a number so don't use accum.bps. accum是一个数字,因此请勿使用accum.bps。 Also, the initial value is a number, so don't use {bps: 0}, just use 0. 另外,初始值是数字,因此请勿使用{bps:0},而应使用0。

arr = [{
    date: "2017-06-14T14:00:00.000Z",
    bps: 2
}, {
    date: "2017-05-14T14:00:00.000Z",
    bps: 9
}, {
    date: "2017-08-14T14:00:00.000Z",
    bps: 4
}]

// Here is my reduce function:

arr.reduce((accum, currVal) => {
    console.log("accum-" + accum);
    console.log("currVal.bps-" + currVal.bps);
    console.log("return value-" + (accum + currVal.bps));
    return accum + currVal.bps;
}, 0);

Here is the output: 这是输出:

accum-0
currVal.bps-2
return value-2
accum-2
currVal.bps-9
return value-11
accum-11
currVal.bps-4
return value-15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM