首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过嵌套的map函数获取no_images/images

如何通过嵌套的map函数获取no_images/images
EN

Stack Overflow用户
提问于 2019-04-01 17:47:31
回答 1查看 61关注 0票数 0

我想使用嵌套的映射函数从no_images中获取图像。

我试过了,但无法获得数据

代码语言:javascript
复制
{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数据为:

代码语言:javascript
复制
[
  {
    "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
  }
]
EN

回答 1

Stack Overflow用户

发布于 2019-04-01 18:15:42

在第一个map函数中,您应该在使用项no_images之前检查它是否存在。根据您的json输出,地图中还有其他项目。

只需在使用对象之前检查它,如下所示:

代码语言:javascript
复制
{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>
     )
   }
)}

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55452223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档