如果您将在控制台中启动此 snipet
[0,1,2,3].reduce((acc, val, index) => {
console.log(index);
return acc;
});
Run Code Online (Sandbox Code Playgroud)
你会得到
1
2
3
Run Code Online (Sandbox Code Playgroud)
所以问题是为什么索引从1开始?
UPD:可能我错过了一些非常基本的东西,但是
[0,1,2,3].reduce((acc, val, index, initialValue) => {
console.log(index);
return acc;
});
Run Code Online (Sandbox Code Playgroud)
给我
1
2
3
0
Run Code Online (Sandbox Code Playgroud)
UPD2:所以是的,我错过了一些基本的东西。
[0,1,2,3].reduce((acc, val, index) => {
console.log(index);
return acc;
}, 0);
Run Code Online (Sandbox Code Playgroud) javascript ×1