简体   繁体   中英

how to convert each element in list to separate dictionary react js

hello i have a list which have all country code i want to convert them into separate dict

code list

(169) ["Af", "al", "dz", "ao", "Ar", "Am", "Au", "At", "Az", "ds", "bh", "Bd", "bb", "by", "be", "bz", "bj", "bt", "bo", "da", "bw", "br", "bn", "bg", "bf", "Bi", "cv", "kh", "cm", "ca", "cf", "td", "cl", "cn", "co", "km", "cg", "cr", "ci", "hr", "cu", "cy", "Cz", "cod", "dk", "Dj", "do", "Ec", "Eg", "Sv", "gq", "er", "ee", "swz", "et", "Fj", "fi", "fr", "ga", "gm", "Ge", "de", "gh", "Gr", "gt", "gn", "gw", "gy", "ht", "hn", "iu", "is", "in", "id", "ir", "ie", "il", "it", "jm", "jp", "jo", "kz", "ke", "kw", "kg", "la", "lv", "lb", "ls", "lr", "ly", "lt", "lu", "mg", "mw", "my", "mv", "ml", "mt", "mr", …]

i want to convert it to like this

 const data = [
    {country: af},
    {country: al},
    {country: dz}.......
 ]

i dont know how to do that.. here is my js code

function current() {
const country_filter_2019 = Data.map((e) => {
    return e.country;
});
console.log(country_filter_2019);
const data = [{ country: country_filter_2019, value: 1424525 }];
console.log(data);
return (
    <div className='map'>
    </div>
);

}

it would be very helpful to have a solution to this..

You need to return an object from your map

const countryMap = data.map(country => ({country}))

Did you mean something like this?

const countries = ["Af", "al", "dz", "ao", "Ar", "Am", "Au"];
const data = countries.map(countryCode => {
    return { country: countryCode };
})
const data = ["Af", "al", "dz", "ao", "Ar", "Am", "Au", "At", "Az", "ds", "bh", "Bd", "bb", "by", "be", "bz", "bj", "bt", "bo", "da", "bw", "br", "bn", "bg", "bf", "Bi", "cv", "kh", "cm", "ca", "cf", "td", "cl", "cn", "co", "km", "cg", "cr", "ci", "hr", "cu", "cy", "Cz", "cod", "dk", "Dj", "do", "Ec", "Eg", "Sv", "gq", "er", "ee", "swz", "et", "Fj", "fi", "fr", "ga", "gm", "Ge", "de", "gh", "Gr", "gt", "gn", "gw", "gy", "ht", "hn", "iu", "is", "in", "id", "ir", "ie", "il", "it", "jm", "jp", "jo", "kz", "ke", "kw", "kg", "la", "lv", "lb", "ls", "lr", "ly", "lt", "lu", "mg", "mw", "my", "mv", "ml", "mt", "mr"].map((code)=>({country:code}))

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