我想使用嵌套的映射函数从no_images中获取图像。
我试过了,但无法获得数据
{this.state.safetyData.map((item, key)=>{
return(
<View>
{item.no_images.map((image,key) => {
return(
<View>
<Text>{JSON.stringify(image.id)}</Text>
</View>
)}
)}
</View>
)}
)}JSON数据为:
[
{
"total_count": 12
},
{
"no_images": [
{
"id": 23,
"title": "Foundation",
"title_phi": "pjilo",
"description": "ok",
"description_phi": "ok",
"image": "1553413849ic_launcher.png",
"type": "1",
"image_path": "uploads/house_images",
"created_at": "2019-03-24 07:50:49",
"updated_at": "2019-03-28 08:51:58",
"house_part_id": 21,
"image_type": 0
}
]
},
{
"no_count": 1
},
{
"percentage": 8
}
]发布于 2019-04-01 18:15:42
在第一个map函数中,您应该在使用项no_images之前检查它是否存在。根据您的json输出,地图中还有其他项目。
只需在使用对象之前检查它,如下所示:
{this.state.safetyData.map((item, key) => {
if (item.no_images) { // HERE: add this check
return(
<View>
{item.no_images.map((image,key) => {
return(
<View>
<Text>{JSON.stringify(image.id)}</Text>
</View>
)}
)}
</View>
)
}
)}希望能有所帮助。
https://stackoverflow.com/questions/55452223
复制相似问题