简体   繁体   中英

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"
    }
]

like this, i have got 236 array of object but i want only few data from it like country, continent, total case, etc and stored it in usetate variable which will be array of object only but it will have this data only

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

if I understand well you have an array with 236 entries. If that's right, here is what you can do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM