【发布时间】:2018-10-11 07:35:34
【问题描述】:
如何在 react native 中映射两个不同的数组?就我而言,我正在从服务器获取响应并对其进行映射。还有另一个名为images 的数组,我想将它与从服务器获取的响应一起列出。但是第二个映射在第一个映射中循环。我如何将它与第一个分开?以下是我的代码。
示例代码
<ScrollView>
{this.state.workers.map(a =>
<CardSection>
<TouchableOpacity onPress={() => this.popupDialog.show()}>
<View style={{ marginTop: 10, marginLeft:120}}>
{images.map(b =>
<Image
style={{ height: 100, width: 100 }}
source={{ uri: b.image }}
/>
)}
<Text style={{marginLeft:20, fontSize:20}}>{a.work_type}</Text>
</View>
</TouchableOpacity>
</CardSection>
)}
workers 数组是我从服务器获取的 json 响应。images 数组如下所示
export const images = [
{
image:'http://localhost:3000/Images/carpenter.png',
text:'hello'
},
{
image:'http://localhost:3000/Images/electrician.png',
text:'hii'
},
]
这也是workers 数组的样子
更新
[
{
"sl.no": 1,
"worker_id": "wr_1",
"work_type": "carpenter",
"phone_num": "3456789243"
},
{
"sl.no": 2,
"worker_id": "wr_2",
"work_type": "electrician",
"phone_num": "345221344"
},
{
"sl.no": 3,
"worker_id": "wr_3",
"work_type": "plumber",
"phone_num": "8976545677"
}
]
【问题讨论】:
-
上面粘贴的代码有什么问题?
-
您是要为每个工人渲染图像组件还是只渲染一次?有点迷茫
-
this.state.workers.map((a, index) => { let b = images[index]; return ... })
标签: javascript react-native jsx array-map