简体   繁体   English

如何在 JS 中遍历嵌套对象

[英]How to iterate over nested objects in JS

I am storing some order details in the firebase database and when I retrieve the data from it, it shows something like below.我在 firebase 数据库中存储了一些订单详细信息,当我从中检索数据时,它显示如下所示。

Array [
  Object {
    "cartItems": Array [
      Object {
        "count": 1,
        "key": "1",
        "name": "EARRINGS",
        "pic": 20,
        "price": "200",
      },
      Object {
        "count": 1,
        "key": "2",
        "name": "Ring",
        "pic": 20,
        "price": "300",
      },
    ],
    "orderId": "OD4242JUL28",
  },
  Object {
    "cartItems": Array [
      Object {
        "count": 1,
        "key": "3",
        "name": "Staple",
        "pic": 20,
        "price": "400",
      },
      Object {
        "count": 1,
        "key": "2",
        "name": "Ring",
        "pic": 20,
        "price": "300",
      },
    ],
    "orderId": "OD4242JUL28",
  },
]

I m able to get the first or second object by using index in [] but after that, if I use map function on it, I am unable to iterate over it.我可以通过在 [] 中使用索引来获得第一个或第二个 object,但在那之后,如果我在其上使用 map function,我将无法对其进行迭代。 I want to retrieve every cartItem and iterate over it to show it in the orders section.我想检索每个 cartItem 并对其进行迭代以在订单部分显示它。

FLATLIST平面列表

<FlatList 
          data={this.state.order}
          keyExtractor={(item) => item.key }
          showsVerticalScrollIndicator={false}
          renderItem={({item}) =>(
            <View>
                <Image source={item.pic} style={{width:width/2.05,height:150}} />
                <Text style={styles.text}>{item.cartItems.name}</Text>
                <Text style={styles.text}>{item.orderId}</Text>
          </View>
          ) }
        />

This only returns orderID but item.cartItems.name returns nothing.这只返回 orderID 但item.cartItems.name返回任何内容。

@Lovepreet Singh try to use recursion method to iterate nested array Of objects. @Lovepreet Singh 尝试使用递归方法来迭代嵌套的对象数组。

Somehow I figured out with the below code不知何故,我想通了下面的代码

<FlatList 
          data={this.state.order}
          keyExtractor={(item) => item.key }
          // numColumns={2}
          showsVerticalScrollIndicator={false}
          renderItem={({item}) =>(
            <View>
                <Image source={item.pic} style={{width:width/2.05,height:150}} />
                <Text style={styles.text}>{item.cartItems.keys}</Text>
                {item.cartItems.map(val=>{
                  return(
                  <View>
                    <Text> {val.count} </Text>
                    <Text> {val.price} </Text>
                    <Text> {val.name} </Text>
                    <Text> {val.pic} </Text>
                    </View> )
                })}
                <Text style={styles.text}>{item.orderId}</Text>
          </View>
          ) }
        />

It uses map function inside the flatlist and I believe there is some wrong with this as it gives a warning Functions are not valid as a React child.它在 flatlist 中使用 map function 我相信这有一些错误,因为它给出了一个警告Functions are not valid as a React child. Please guide me the best way to do this.请指导我执行此操作的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历 firestore 中的所有文档? - How to iterate over all documents in firestore? 如何从 Firestore 解析嵌套对象? - How to parse nested objects from Firestore? 如何连续将对象添加到 Firestore 中的嵌套集合 - How to continuously add objects to nested collection in Firestore Objectify - 我们可以为 QueryKeys 设置 startAt(cursor) 吗? 或者如何迭代许多 Keys 查询? - Objectify - can we set a startAt(cursor) for QueryKeys? Or how to iterate over many Keys query? Terraform - 迭代组合 map 和列表 - Terraform - iterate over combined map and list 嵌套迭代调解器在 WSO2 esb 中不起作用 - Nested Iterate Mediator is not working in WSO2 esb iOS Swift 迭代 Firestore 中的字典项数组 - iOS Swift iterate over an Array of Dictionary Items from Firestore Flutter 搜索嵌套对象列表 - Flutter search nested list of objects AppSync/GraphQL 过滤嵌套对象 - AppSync/GraphQL filter nested objects 我如何迭代 boto3 中的嵌套字典和列表以获得特定值? - How can I itterate over nested dictionaries and lists in boto3 to obtain particular values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM