React Native - How to pass index in map function

2020-06-28 02:58发布

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.

3条回答
啃猪蹄的小仙女
2楼-- · 2020-06-28 03:15

Simply pass a second arguments to your arrow function (data, index)

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

Signaure for .map

var new_array = arr.map(function callback(currentValue, index, array) {
    // Return element for new_array
}[, thisArg])
查看更多
贪生不怕死
3楼-- · 2020-06-28 03:19

Map provides second argument as the index of the current element and third argument as the whole array itself.

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

Read more about Array.prototype.map

查看更多
Melony?
4楼-- · 2020-06-28 03:20

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

查看更多
登录 后发表回答