reactjs - country-data map

时间:2018-12-19 11:26:29

标签: reactjs country-codes

I am receiving an array of country codes as props like this: ['en', 'ro', 'da']. What would be the best way to map it back to their names in an object like this: {value: 'en', label: 'English}.

1 个答案:

答案 0 :(得分:0)

尝试这种模式:

const myCountries = ['en', 'ro']; 
const data= [{EN: "England"}, {RO: "Romania"}, {DA: "Denmark"}];

const getCountriesOption = () => {
const countriesOptions = data.map(
  country => ({ value: Object.keys(country)[0], label: country[Object.keys(country)[0]] }))
      
  return countriesOptions;
    }
// All options
console.log(getCountriesOption());
// Only my countries
console.log(getCountriesOption().filter(
obj => myCountries.includes(obj.value.toLowerCase())));