简体   繁体   English

React Native - 如何在地图函数中传递索引

[英]React Native - How to pass index in map function

I have a map function to create a component repetitively and dynamically.我有一个映射函数来重复和动态地创建一个组件。 Suppose it's like this:假设它是这样的:

renderBoxes() {
    return Array.map(data => this.myFunction(indexOfThisArray));
} 

How can I pass the index of the array?如何传递数组的索引? So that the 'myFunction' function gets the index value everytime it is called.这样 'myFunction' 函数每次被调用时都会得到索引值。

Map provides second argument as the index of the current element and third argument as the whole array itself. Map 提供第二个参数作为当前元素的索引,第三个参数作为整个数组本身。

renderBoxes() {
    return Array.map((data, index, array) => this.myFunction(index));
} 

Read more about Array.prototype.map阅读有关Array.prototype.map 的更多信息

the syntax of map is地图的语法是

var new_array = arr.map(function callback(currentValue, index, array) {
    // Return element for new_array
}[, thisArg])

source . 来源 You can find the index as the 2nd parameter in the callback function您可以在回调函数中找到索引作为第二个参数

Simply pass a second arguments to your arrow function (data, index)只需将第二个参数传递给您的箭头函数(data, index)

renderBoxes() {
    return Array.map((data, index) => this.myFunction(indexOfThisArray));
} 

Signaure for .map .map签名

var new_array = arr.map(function callback(currentValue, index, array) {
    // Return element for new_array
}[, thisArg])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM