React Native-如何在地图函数中传递索引 [英] React Native - How to pass index in map function

查看:98
本文介绍了React Native-如何在地图函数中传递索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个map函数来重复动态地创建一个组件.假设是这样的:

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));
} 

如何传递数组的索引?这样,"myFunction"函数就可以在每次调用时获取索引值.

How can I pass the index of the array? So that the 'myFunction' function gets the index value everytime it is called.

推荐答案

Map提供第二个参数作为当前元素的索引,第三个参数作为整个数组本身.

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));
} 

详细了解 Array.prototype.地图

这篇关于React Native-如何在地图函数中传递索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆