【发布时间】:2022-01-21 00:23:29
【问题描述】:
我正在使用来自 API 的数据创建一个 React 应用程序。响应数据是一个长而复杂的对象,看起来像这样:
{
"Frozen Products": [
{
"_id": "6181849285e8d8f86be2d9df",
"name": "Peas (800g)",
"category_id": "6181841060c425f76e57b603",
"slug": "vegetables",
"quantity": 16,
"price": {
"amount": 3.00
},
"thumbnail":"URI ...",,
"images": [
"URI ...",
],
"synonyms": [
"Veggies"
],
},
//etc for many key/values inside this object ..
}
我需要访问的信息确实显示一个类别(在本例中为“冷冻产品”)及其下的所有产品名称和图像。
但我认为我需要通过它们的索引(示例数据 [0])而不是键名称循环访问每个键/值对,因为它们的键是由 API 动态创建的。那么我该如何将其更改为:
{
[ "Frozen Products": [
{
"_id": "6181849285e8d8f86be2d9df",
"name": "Peas (800g)",
"category_id": "6181841060c425f76e57b603",
"slug": "vegetables",
"quantity": 16,
"price": {
"amount": 3.00
},
"thumbnail":"URI ...",,
"images": [
"URI ...",
],
"synonyms": [
"Veggies"
],
},
]
【问题讨论】:
-
您的预期输出不是有效的 JSON。你能改正吗?
标签: javascript reactjs axios