admin管理员组

文章数量:1400128

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

本文标签: Reduce in javascript with initial valueStack Overflow