Why JavaScript reduce method current index parameter starts from 1? - Stack Overflow

If you will launch this snipet it in your console[0,1,2,3].reduce((acc, val, index) => {console.log(

If you will launch this snipet it in your console

[0,1,2,3].reduce((acc, val, index) => {
  console.log(index);
  return acc;
});

You will get

1
2
3

So the question is why index starts from 1?

UPD: Probably I missing something very basic, but

[0,1,2,3].reduce((acc, val, index, initialValue) => {
  console.log(index);
  return acc;
});

gives me

1
2
3
0

UPD2: So yes, it's me missing something basic.

[0,1,2,3].reduce((acc, val, index) => {
  console.log(index);
  return acc;
}, 0);

If you will launch this snipet it in your console

[0,1,2,3].reduce((acc, val, index) => {
  console.log(index);
  return acc;
});

You will get

1
2
3

So the question is why index starts from 1?

UPD: Probably I missing something very basic, but

[0,1,2,3].reduce((acc, val, index, initialValue) => {
  console.log(index);
  return acc;
});

gives me

1
2
3
0

UPD2: So yes, it's me missing something basic.

[0,1,2,3].reduce((acc, val, index) => {
  console.log(index);
  return acc;
}, 0);
Share Improve this question edited Feb 20, 2020 at 14:21 Volod asked Feb 20, 2020 at 14:09 VolodVolod 1,4372 gold badges19 silver badges35 bronze badges 6
  • 4 This is answered in the documentation on MDN: "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 and skipped." – user47589 Commented Feb 20, 2020 at 14:12
  • @JaredSmith Yeah, missing something in the docs or assuming the worst in someone isn't a reason for a bad question imo – bananabrann Commented Feb 20, 2020 at 14:16
  • 1 @bananabrann I'll restate the core of my objection: I realize this is something about which reasonable people could have different opinions. I disagree with yours, but I don't think it's so wrong that it's worth calling you names in public over... downvotes (to me at least) are not personal, the way, say, name-calling is. – Jared Smith Commented Feb 20, 2020 at 14:19
  • @bananabrann You can disagree with people without calling them petty. – user47589 Commented Feb 20, 2020 at 14:23
  • 1 If you’re upset about the petty remark, that I’m sorry for. Though I still hold my original thinking, I did not mean an intentional attack of character – bananabrann Commented Feb 20, 2020 at 14:44
 |  Show 1 more ment

1 Answer 1

Reset to default 7

Because you have not provided an initialValue argument. From the docs:

If no initialValue is supplied, the first element in the array will be used and skipped

And reading further, there's a direct answer to your question:

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.

More info here

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信