简体   繁体   中英

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

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

code list

this is name 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", …]

this is value list

const value = [
    '11 000',
    '1400 [730–2800]',
    '22 000 [10 000–32 000]',
    '340 000 [290 000–410 000]',
    '140 000 [120 000–160 000]',
    '3500 [2900–4500]',
    '29 000 [21 000–38 000]',
    'No data',
    '9700 [7300–13 000]',
    'No data',
    'No data',
    'No data',
    '2700 [1300–4700]',
    '28 000 [22 000–36 000]',
    'No data',
    'No data',
    '75 000 [50 000–110 000]',
    'No data',
    '19 000 [16 000–23 000]',
    'No data',
    '380 000 [340 000–410 000]',
    '920 000 [420 000–1 300 000]',
    'No data',
    '3300 [3000–3700]',
    '100 000 [84 000–120 000]',
    '85 000 [73 000–100 000]',
    '2500 [2100–3000]',
    '73 000 [63 000–84 000]',
    '510 000 [450 000–560 000]',
    'No data',
    '100 000 [84 000–130 000]',
    '120 000 [94 000–140 000]',
    '74 000 [64 000–85 000]',
    'No data',
    '200 000 [160 000–250 000]',
    '130 [<100–<500]',
    '100 000 [78 000–140 000]',
    '14 000 [11 000–18 000]',
    '430 000 [370 000–500 000]',
    '1600 [1100–2100]',
    '32 000 [20 000–47 000]',
    'No data',
    'No data',
    '520 000 [420 000–640 000]',
    'No data',
    '6800 [4900–10 000]',
    '72 000 [58 000–88 000]',
    '47 000 [35 000–78 000]',
    '26 000 [19 000–34 000]',
    '27 000 [23 000–32 000]',
    '65 000 [48 000–88 000]',
    '14 000 [10 000–19 000]',
    'No data',
    '200 000 [190 000–220 000]',
    '670 000 [510 000–860 000]',
    '1000 [670–2200]',
    'No data',
    '190 000 [160 000–220 000]',
    '51 000 [38 000–66 000]',
    '28 000 [22 000–36 000]',
    '9100 [5700–15 000]',
    'No data',
    '340 000 [250 000–460 000]',
    'No data',
    '36 000 [31 000–44 000]',
    '110 000 [95 000–130 000]',
    '40 000 [35 000–46 000]',
    '8700 [7800–9700]',
    '160 000 [130 000–180 000]',
    '25 000 [21 000–29 000]',
    'No data',
    'No data',
    'No data',
    'No data',
    '59 000 [33 000–130 000]',
    '7500 [6200–8800]',
    'No data',
    '130 000 [71 000–210 000]',
    '32 000 [26 000–44 000]',
    'No data',
    'No data',
    '33 000 [30 000–38 000]',
    '1 500 000 [1 300 000–1 700 000]',
    'No data',
    '10 000 [9700–10 000]',
    '13 000 [12 000–15 000]',
    '5600 [3500–7400]',
    '2700 [1500–4300]',
    '340 000 [320 000–360 000]',
    '47 000 [37 000–59 000]',
    '9500 [7200–12 000]',
    '3400 [2900–4000]',
    'No data',
    '39 000 [32 000–49 000]',
    '1 100 000 [960 000–1 100 000]',
    '88 000 [78 000–98 000]',
    'No data',
    '140 000 [120 000–180 000]',
    'No data',
    '5700 [4200–8300]',
];

i want to convert them like

 const data = [
{country: af, value: "11000"},
{country: al, value: "1400"},
{country: dz, value: "22000"}.......

]

it would be very helpful to have a solution to this.. i have been looking for solution for a long time.. i want a dictionay like the example above. when i try it is loading all the data in one array

Refer to -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

This can be achieved using map method which is described as " The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. "

In this case the provide function is:

(currentValue, index) => ({'country': countryList[index], 'value': currentValue})

Note: make sure to validate that both are of same size or if not what to do.

Putting it altogether:

 const countryList = ["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"]; const value = [ '11 000', '1400 [730–2800]', '22 000 [10 000–32 000]', '340 000 [290 000–410 000]', '140 000 [120 000–160 000]', '3500 [2900–4500]', '29 000 [21 000–38 000]', 'No data', '9700 [7300–13 000]', 'No data', 'No data', 'No data', '2700 [1300–4700]', '28 000 [22 000–36 000]', 'No data', 'No data', '75 000 [50 000–110 000]', 'No data', '19 000 [16 000–23 000]', 'No data', '380 000 [340 000–410 000]', '920 000 [420 000–1 300 000]', 'No data', '3300 [3000–3700]', '100 000 [84 000–120 000]', '85 000 [73 000–100 000]', '2500 [2100–3000]', '73 000 [63 000–84 000]', '510 000 [450 000–560 000]', 'No data', '100 000 [84 000–130 000]', '120 000 [94 000–140 000]', '74 000 [64 000–85 000]', 'No data', '200 000 [160 000–250 000]', '130 [<100–<500]', '100 000 [78 000–140 000]', '14 000 [11 000–18 000]', '430 000 [370 000–500 000]', '1600 [1100–2100]', '32 000 [20 000–47 000]', 'No data', 'No data', '520 000 [420 000–640 000]', 'No data', '6800 [4900–10 000]', '72 000 [58 000–88 000]', '47 000 [35 000–78 000]', '26 000 [19 000–34 000]', '27 000 [23 000–32 000]', '65 000 [48 000–88 000]', '14 000 [10 000–19 000]', 'No data', '200 000 [190 000–220 000]', '670 000 [510 000–860 000]', '1000 [670–2200]', 'No data', '190 000 [160 000–220 000]', '51 000 [38 000–66 000]', '28 000 [22 000–36 000]', '9100 [5700–15 000]', 'No data', '340 000 [250 000–460 000]', 'No data', '36 000 [31 000–44 000]', '110 000 [95 000–130 000]', '40 000 [35 000–46 000]', '8700 [7800–9700]', '160 000 [130 000–180 000]', '25 000 [21 000–29 000]', 'No data', 'No data', 'No data', 'No data', '59 000 [33 000–130 000]', '7500 [6200–8800]', 'No data', '130 000 [71 000–210 000]', '32 000 [26 000–44 000]', 'No data', 'No data', '33 000 [30 000–38 000]', '1 500 000 [1 300 000–1 700 000]', 'No data', '10 000 [9700–10 000]', '13 000 [12 000–15 000]', '5600 [3500–7400]', '2700 [1500–4300]', '340 000 [320 000–360 000]', '47 000 [37 000–59 000]', '9500 [7200–12 000]', '3400 [2900–4000]', 'No data', '39 000 [32 000–49 000]', '1 100 000 [960 000–1 100 000]', '88 000 [78 000–98 000]', 'No data', '140 000 [120 000–180 000]', 'No data', '5700 [4200–8300]', ]; const output = value.map((currentValue, index) => ({ 'country': countryList[index], 'value': currentValue })); console.log(output);

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