javascript - 如何在本机反应中同时映射两个数组?
问题描述
如何在本机反应中映射两个不同的数组?就我而言,我正在从服务器获取响应并对其进行映射。还有另一个名为的数组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
array 是我从服务器获取的 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"
}
]
解决方案
如果图像未加载,可能是因为您将错误的属性传递给<Image />
组件。查找<Image />
组件的文档或将其替换为<img />
,并将图像的 url 字符串传递给src
属性。
getImageUri(worker) {
// Decide which image to return for a certain type of worker
// For more workers and images, change the following logic
if(worker.work_type == 'carpenter') {
return images[0].image;
} else if(worker.work_type == 'electrician') {
return images[1].image;
} else {
return '';
}
}
render() {
...
<ScrollView>
{this.state.workers.map((a, index) =>
<CardSection>
<TouchableOpacity onPress={() => this.popupDialog.show()}>
<View style={{ marginTop: 10, marginLeft:120}}>
<Image
style={{ height: 100, width: 100 }}
source={{ uri: this.getImageUri(a)}}
/>
<Text style={{marginLeft:20, fontSize:20}}>{a.work_type}</Text>
</View>
</TouchableOpacity>
</CardSection>
)}
</ScrollView>
...
}
推荐阅读
- database - 在 Postgres 中存储加密数据最合适的方式
- python - ValueError:托管端点错误。生产变体 AllTraffic 的主容器未通过 ping 健康检查
- windows-installer - 错误代码 1703,没有消息和选项,按 OK 后强制重启
- c# - 如何将具有属性的类传递给 WPF 模式窗口,对其进行修改,然后将类返回父窗口?
- regex - 在字符串前后添加一个单词
- c++ - strcpy 可以在 Windows 上编译,但不能在 Linux 上编译
- java - JavaFX:FileChooser 闪烁
- django - Django:向所有用户(会话)发送消息
- ios - XCUITest/Appium 在性能测试期间在 iOS 上的应用程序上加载
- javascript - 找不到方法 moveThreadToInbox((class))