gpt4 book ai didi

javascript - 使用具有两个输入的箭头函数调用 Array.prototype.map() 的目的是什么?

转载 作者:行者123 更新时间:2023-11-28 17:43:26 25 4
gpt4 key购买 nike

我正在关注 React.js 教程,目前位于 https://reactjs.org/tutorial/tutorial.html#showing-the-moves 部分我在 React.Componentrender 方法中看到以下代码片段:

const moves = history.map((step, move) => {
const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});

这里,history 是具有 square 属性的对象数组:

history = [
{
squares: [
null, null, null,
null, null, null,
null, null, null,
]
},
{
squares: [
null, null, null,
null, 'X', null,
null, null, null,
]
},
// ...
]

我不明白的是为什么history.map中的箭头函数有两个输入,stepmove。在 map 的文档中,https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map ,我只看到带有一个输入(即相关数组元素)的箭头函数的示例。我也没有看到箭头方法主体中任何地方使用的 step 输入,所以我不确定它的目的是什么。有人能解释一下吗?

最佳答案

在此示例中,move 是每个步骤的索引。使用此索引,您可以找到历史记录中的指定步骤。

关于javascript - 使用具有两个输入的箭头函数调用 Array.prototype.map() 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com