簡體   English   中英

如何 map 我從 api 調用 usestate 變量獲得的 object 數組所需的數據

[英]How to map the required data of array of object which i got from api call to usestate variable


[
    {
        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 個 object 數組,但我只想要它的少數數據,如國家、大陸、總案例等,並將其存儲在 usetate 變量中,該變量將是 object 的數組,但它只會有這個數據

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
    }
})

如果我理解得很好,您有一個包含 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 }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM