React Native:遍历嵌套对象以显示值 [英] React Native: Iterate through nested objects to display values

查看:93
本文介绍了React Native:遍历嵌套对象以显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在屏幕上显示多个图像.我的数据库树如下所示:

I want to display multiple images into a screen. My database tree looks like this:

我还能够通过snapshot.val()加载对象,这就是运行时在控制台中的样子:

I am also able to load the object through snapshot.val() and this is how it looks like in the console while running:

Object {
  "4aae-47bb-e0f7-36e2-7e66": Object {
    "author": "edm9AAbPpFUWrO9HDXfV442QzSE2",
    "caption": "Test 1",
    "photo": Object {
      "2e30-b971-5c62-0b68-837f": Object {
        "url": "https://firebasestorage.googleapis.com/v0/b/...someURL...",
      },
      "38de-15f2-bb7b-b58d-e863": Object {
        "url": "https://firebasestorage.googleapis.com/v0/b/...someURL...",
      },
      "94f2-1494-908f-c17a-5adc": Object {
        "url": "https://firebasestorage.googleapis.com/v0/b/...someURL...",
      },
    },
    "posted": 1562901067,
    "vote": 1,
  },
}

我想遍历所有"url"值,以便可以显示所有图像.到目前为止,这是我的方法:

I want to iterate through all the "url" values so I can display all the images. This was my approach so far:

          <FlatList
            refreshing={this.state.refresh}
            onRefresh={this.loadNew}
            data={this.state.photo_feed}
            keyExtractor={(item, index) => index.toString()}
            renderItem={({ item, index }) => (
              <View key={index}>
                <View>
                  <TouchableOpacity>
                    <View>
                    {
                      Object.values(item.photo).map(photoItem => (
                        <Image source={{ uri: photoItem.url }} />
                      )) 
                    }
                    </View>
                  </TouchableOpacity>
                </View>
              </View>

            )}
          />

运行Object.value函数时出现此错误:

I am getting this error when running my Object.value function:

TypeError: Object.values requires that input parameter not be null or undefined

推荐答案

如果只想遍历照片,则无需提供整个 photo_feed 对象.

If you want to iterate over only Photos then you don't need to provide the whole photo_feed object.

您只能提供包含照片的子对象.像这样

You can just supply only the child object containing the photos. Like this,

data={this.state.photo_feed.photo}

然后,这些项目应使用相同的代码呈现.

After that, the items should render with the same code.

这篇关于React Native:遍历嵌套对象以显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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