admin管理员组文章数量:1426016
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
1 Answer
Reset to default 7Because 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 index1
, skipping the first index. IfinitialValue
is provided, it will start at index0
.
More info here
本文标签: Why JavaScript reduce method current index parameter starts from 1Stack Overflow
版权声明:本文标题:Why JavaScript reduce method current index parameter starts from 1? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745470150a2659711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论