admin管理员组文章数量:1208155
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.
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.
Share Improve this question edited Sep 4, 2017 at 18:20 imjared 20.6k4 gold badges55 silver badges75 bronze badges asked Sep 4, 2017 at 18:13 prasang7prasang7 5712 gold badges11 silver badges31 bronze badges 1- 1 at least you saved my some time lol – Bikram Thapa Commented Feb 18, 2019 at 6:22
3 Answers
Reset to default 15Map 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
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)
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])
本文标签: javascriptReact NativeHow to pass index in map functionStack Overflow
版权声明:本文标题:javascript - React Native - How to pass index in map function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738704978a2107854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论