Reduce in javascript with initial value - Stack Overflow

I am trying to reduce an array to the sum of its even values. I have been checking on the document from

I am trying to reduce an array to the sum of its even values. I have been checking on the document from MDN -

This says that if the initial value is provided then it won't skip the 0th index, in fact it will start from index 0. My problem is that the reduce is starting with index 1. Thus, my result is incorrect. I am sure I am reading the document incorrectly or misunderstanding it. This is the note I am referring to - "Note: If initialValue is not provided, reduce() will execute the callback function starting at index 1, skipping the first index. If initialValue is provided, it will start at index 0."

Here is my code.

var array = [1,2,3,4,6,100];
var initialValue = 0;
var value = array.reduce(function(accumulator, currentValue, currentIndex, array, initialValue) {
    //console.log(accumulator);
    if( currentValue % 2 === 0) {
      accumulator += currentValue;
      //return accumulator;
    }
    return accumulator;
});
console.log(value);

Obviously, I am seeing the result 113 and not 112. I guess, it is because accumulator already has a value 1. Thus, it is adding 1 initially.

I am trying to reduce an array to the sum of its even values. I have been checking on the document from MDN - https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

This says that if the initial value is provided then it won't skip the 0th index, in fact it will start from index 0. My problem is that the reduce is starting with index 1. Thus, my result is incorrect. I am sure I am reading the document incorrectly or misunderstanding it. This is the note I am referring to - "Note: If initialValue is not provided, reduce() will execute the callback function starting at index 1, skipping the first index. If initialValue is provided, it will start at index 0."

Here is my code.

var array = [1,2,3,4,6,100];
var initialValue = 0;
var value = array.reduce(function(accumulator, currentValue, currentIndex, array, initialValue) {
    //console.log(accumulator);
    if( currentValue % 2 === 0) {
      accumulator += currentValue;
      //return accumulator;
    }
    return accumulator;
});
console.log(value);

Obviously, I am seeing the result 113 and not 112. I guess, it is because accumulator already has a value 1. Thus, it is adding 1 initially.

Share Improve this question asked Mar 26, 2021 at 23:05 tannoy connecttannoy connect 3851 gold badge6 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

If you have a look again at the document, you will notice that the callbackFn only takes at most 4 variables, and the initialValue must be on the second parameter

arr.reduce(callback( accumulator, currentValue, [, index[, array]] )[, initialValue])
                   ^                                               ^

Here is the small-fixed version which returned 112 as expected

var array = [1,2,3,4,6,100];
var initialValue = 0;
var value = array.reduce(function(accumulator, currentValue, currentIndex, array) {
    //console.log(accumulator);
    if( currentValue % 2 === 0) {
      accumulator += currentValue;
      //return accumulator;
    }
    return accumulator;
}, initialValue);
console.log(value);

This is the syntax provided by MDN for using an initial value:

 [0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => {
    return accumulator + currentValue}, 10)

The initial value in this example is 10, it should be passed as a second argument to the reduce function.

You're not passing the initial value parameter to the reduce function.

Like @hgb123 explained, it is the second parameter.

Futhermore, you don't need to pass all the parameters, if you don't need them. Keep your functions as simple as possible, so they'll be easier to read:

var array = [1,2,3,4,6,100];

var value = array.reduce(function(accumulator, currentValue) {
    //console.log(accumulator);
    if( currentValue % 2 === 0) {
      accumulator += currentValue;
      //return accumulator;
    }
    return accumulator;
}, 0);

console.log(value);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744238746a4564601.html

相关推荐

  • Reduce in javascript with initial value - Stack Overflow

    I am trying to reduce an array to the sum of its even values. I have been checking on the document from

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信