【问题标题】:How to fetch array inside another object using Axios?如何使用 Axios 在另一个对象中获取数组?
【发布时间】:2021-04-24 18:24:46
【问题描述】:

这是我的 API,我想使用 axios 获取它

[
    {
        "groupType": "Interessegruppe",
        "groups": [
            {
                "id": "c42b0250-375e-4458-8f68-0df8179f889c",
                "name": "Teater",
                "assignmentStrategy": 1
            },
            {
                "id": "0fb94732-2bb1-483b-abbd-858327339ca0",
                "name": "Fotball",
                "assignmentStrategy": 1
            },
            {
                "id": "6b109ab1-5a3e-46b0-b0cc-e2b48d6e2dc6",
                "name": "Foto",
                "assignmentStrategy": 1
            }
        ]
    },

]

我写了那个代码

      renderItem={({ item }) => <GroupCard title={item.groupType} subTitle={item.groups.name} />}
      />

我获取了 groupType,但我无法获取任何 [id 或 name]

【问题讨论】:

  • item.groups是一个数组,所以你需要使用list rendering(没有item.groups.name,但是有item.groups[0].name例子)

标签: javascript arrays react-native api axios


【解决方案1】:

async function loadData() {
    const data = await axios.get(url)
    return data[0].groups;
}

此函数将返回具有idname 属性的组数组。

【讨论】:

    猜你喜欢
    • 2022-12-05
    • 2019-06-16
    • 2021-11-09
    • 2022-11-27
    • 1970-01-01
    • 2019-09-17
    • 2021-06-15
    • 1970-01-01
    • 2021-10-09
    相关资源
    最近更新 更多