How reduce in javascript work if initial value is array - Stack Overflow

var a = [2,4,5,6,7]a.reduce((acc,cur,i)=>acc,[])result : []a.reduce((acc,cur,i)=>acc,[0,2])

var a = [2,4,5,6,7]
a.reduce((acc,cur,i)=>acc,[])
//result : []
a.reduce((acc,cur,i)=>acc,[0,2])
//result (2) [0, 2]
a.reduce((acc,cur,i)=>acc[0],[0,2])
/*
VM410:1 Uncaught TypeError: Cannot read property '0' of undefined
    at <anonymous>:1:26
    at Array.reduce (<anonymous>)
    at <anonymous>:1:3
    */

var a = [2,4,5,6,7]
a.reduce((acc,cur,i)=>acc,[])
//result : []
a.reduce((acc,cur,i)=>acc,[0,2])
//result (2) [0, 2]
a.reduce((acc,cur,i)=>acc[0],[0,2])
/*
VM410:1 Uncaught TypeError: Cannot read property '0' of undefined
    at <anonymous>:1:26
    at Array.reduce (<anonymous>)
    at <anonymous>:1:3
    */

why am I getting error for the third one? Wondering how it works?

Share Improve this question asked Apr 12, 2020 at 16:20 azhahes.sazhahes.s 351 silver badge6 bronze badges 4
  • 2 First iteration acc = [0, 2] you return 0 (the number). Second iteration acc = 0 – VLAZ Commented Apr 12, 2020 at 16:21
  • 1 your return value on each iteration bees acc for next iteration, so when you do acc[0] which is 0 so on next iteration you're doing 0[0] which returns undefined and then undefined[0] is syntax error – Code Maniac Commented Apr 12, 2020 at 16:25
  • @CodeManiac no 0[0] is fine - it produces undefined. Next iteration there us a TypeError because of undefined[0] – VLAZ Commented Apr 12, 2020 at 16:29
  • @VLAZ ahh thanks for pointing fixed my ment – Code Maniac Commented Apr 12, 2020 at 16:30
Add a ment  | 

3 Answers 3

Reset to default 3

Your code does not return an array. However, the accumulator will be set to that value. So while you assume that acc is an array, it is not after the first call to the accumulation function.

What you can do instead is something along the following lines:

    var a = [2,4,5,6,7]; 
    a.reduce((acc,cur,i) => {
            acc.push(cur);
            console.log(`iter ${i}, acc = ${JSON.stringify(acc)}.`);
            return acc;
       }
      ,[0,2]
    );

Just make sure that your callback returns an array upon each invocation.

The reduce function works as a for loop, where a variable (acc) gets set at every iteration:

The first two examples you gave:

var a = [2,4,5,6,7]

a.reduce((acc,cur,i) => acc, []) 
a.reduce((acc,cur,i) => acc, [0, 2])

Are equivalent to assigning the accumulator to itself at each iteration:

var acc = []; // [0, 2] in the second one

for(var i=0; i < a.length; i++){
    acc = acc;
}

Whereas your last example:

a.reduce((acc,cur,i) => acc[0], [0, 2])

Is equivalent to assigning to acc its first value at each iteration:

var acc = [0, 2];

for(var i = 0; i < a.length; i++){
    acc = acc[0];
}

Each iteration will assign to acc its first value. The first iteration will look like this:

acc = [0, 2];
i = 0

acc = acc[0] // 0;

The second iteration will then look like this:

acc = 0;
i = 1

acc = acc[0] // undefined;

Now acc has been assigned the value undefined as there is no such property 0 in the value held by acc. The third iteration will, therefore, look like this:

acc = undefined;
i = 2;

acc = acc[0]; // Error, no property '0' of undefined.

An excerpt from mdn documentation for Array.reduce

initialValue -A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue. Calling reduce() on an empty array without an initialValue will throw a TypeError.

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

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

相关推荐

  • How reduce in javascript work if initial value is array - Stack Overflow

    var a = [2,4,5,6,7]a.reduce((acc,cur,i)=>acc,[])result : []a.reduce((acc,cur,i)=>acc,[0,2])

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信