【问题标题】:How to map the required data of array of object which i got from api call to usestate variable如何将我从 api 调用中获得的对象数组的所需数据映射到 usestate 变量
【发布时间】:2022-01-10 16:48:56
【问题描述】:

[
    {
        cases: {new: '+44', active: 216, critical: 1, recovered: 15364, 1M_pop: '35323', …}
        continent: "Asia"
        country: "Brunei"
        day: "2022-01-10"
        deaths: {new: null, 1M_pop: '221', total: 98}
        population: 443851
        tests: {1M_pop: '1562743', total: 693625}
        time: "2022-01-10T16:15:04+00:00"
    },
    { 
        cases: {new: '+2', active: 4, critical: null, recovered: 20, 1M_pop: '34', …}
        continent: "Oceania"
        country: "Solomon-Islands"
        day: "2022-01-10"
        deaths: {new: null, 1M_pop: null, total: null}
        population: 711920
        tests: {1M_pop: '6321', total: 4500}
        time: "2022-01-10T16:15:04+00:00"
    }
]

像这样,我有 236 个对象数组,但我只想要其中的少量数据,例如国家、大陆、总案例等,并将其存储在 usetate 变量中,该变量将仅是对象数组,但它只会包含此数据

【问题讨论】:

标签: javascript reactjs api


【解决方案1】:
let response = [
    {
        cases: { new: '+44', active: 216, critical: 1, recovered: 15364, 1M_pop: '35323', … }
        continent: "Asia"
        country: "Brunei"
        day: "2022-01-10"
        deaths: { new: null, 1M_pop: '221', total: 98 }
        population: 443851
        tests: { 1M_pop: '1562743', total: 693625 }
        time: "2022-01-10T16:15:04+00:00"
    },
    {
        cases: { new: '+2', active: 4, critical: null, recovered: 20, 1M_pop: '34', … }
        continent: "Oceania"
        country: "Solomon-Islands"
        day: "2022-01-10"
        deaths: { new: null, 1M_pop: null, total: null }
        population: 711920
        tests: { 1M_pop: '6321', total: 4500 }
        time: "2022-01-10T16:15:04+00:00"
    }
];
let mappedResponse = response.map(obj => {
    const { country, continent } = obj;
    return {
        country, 
        continent
    }
})

【讨论】:

    【解决方案2】:

    如果我理解得很好,您有一个包含 236 个条目的数组。如果正确,您可以执行以下操作:

    const results = [
        {
            cases: {new: '+44', active: 216, critical: 1, recovered: 15364, 1M_pop: '35323', …}
            continent: "Asia"
            country: "Brunei"
            day: "2022-01-10"
            deaths: {new: null, 1M_pop: '221', total: 98}
            population: 443851
            tests: {1M_pop: '1562743', total: 693625}
            time: "2022-01-10T16:15:04+00:00"
        },
        { 
            cases: {new: '+2', active: 4, critical: null, recovered: 20, 1M_pop: '34', …}
            continent: "Oceania"
            country: "Solomon-Islands"
            day: "2022-01-10"
            deaths: {new: null, 1M_pop: null, total: null}
            population: 711920
            tests: {1M_pop: '6321', total: 4500}
            time: "2022-01-10T16:15:04+00:00"
        }
    ];
    
    const newArray = results.map(m => {
       return { country: m.country, cases: m.cases }
    })
    

    【讨论】:

    • 兄弟我想把这个结果添加到usestate钩子变量中,并作为props传递到另一个组件中以显示在表格中。
    猜你喜欢
    • 2023-01-13
    • 2021-11-06
    • 2014-07-12
    • 2020-09-24
    • 2022-11-18
    • 2023-04-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    相关资源
    最近更新 更多