JSON访问javascript中一个数字命名元素的元素和嵌套对象



我从API获得以下JSON响应:

[{
"order_id":"1111",
"restaurant_id":"ABC",
"fullfillment":"Delivery",
"order_channel":"iOS",
"0":[{
"id":"678",
"info":"long string",
"created_at":"2022-02-12T04:34:51.000000Z",
"item":[
{"id":328,
"order_id":"123",
"item_sold":"item number 1",
"item_price":"10.0",
"topping":[{"id":987,
"item_id":"263",
"topping_ordered_name":"Name of x topping",
"topping_paid_price":"2.0"},
{"id":988,
"item_id":"633",
"topping_ordered_name":"Name of y topping",
"topping_paid_price":"1.0"},
{"id":989,
"item_id":"453",
"topping_ordered_name":"Name of z topping",
"topping_paid_price":"3.0"}]
},
{"id":329,
"order_id":"123",
"item_sold":"item number 2",
"item_price":"12.0",
"topping":[{"id":768,
"item_id":"432",
"topping_ordered_name":"Name of a topping",
"topping_paid_price":"2.0"},
{"id":988,
"item_id":"763",
"topping_ordered_name":"Name of b topping",
"topping_paid_price":"1.0"},
{"id":989,
"item_id":"773",
"topping_ordered_name":"Name of c topping",
"topping_paid_price":"1.0"}]
}
]
}]
}]

我正在React应用程序中检索值,如下所示:

<tbody>
{this.state.sales.map((sale) => (
<tr key={sale.order_id} tabIndex="0">
<td>
{sale.restaurant_id}
{sale.fullfillment}
{sale.order_channel}
</td>
</tr>
))}
</tbody>

我如何访问嵌套在";0";包括嵌套的元素和对象?

实例";0〃;。id;0〃;。info和一个嵌套对象;0〃;。item.order_ id或";0〃;。item.item_ sold或甚至进一步嵌套的对象;0〃;。item.toppin_topping_ordered_name

我对";0";并且我不确定如何访问它;0";并且以后不会变为"0";1〃;或者其他,但如果我遇到其他像数字一样命名的物体,";0"1〃"2〃;。。。sale.0无效,sale[0]不返回任何结果。

编辑:如果我console.log(sale[0](,它会返回:

未捕获错误:对象作为React子对象无效(已找到:键为{id,info,created_at,item}的对象(。如果您想要呈现子集合,请使用数组而不是

和if i console.log(sale[0].info(

未捕获类型错误:无法读取未定义的属性"info">

似乎是一个多维数组sale[0][0].id,sale[0][0].info应该返回值。

最新更新