reactjs - 国家数据地图
reactjs - country-data map
我收到了一组国家代码作为道具,如下所示:['en', 'ro', 'da']
。在这样的对象中将其映射回他们的名字的最佳方法是什么:{value: 'en', label: 'English}
.
试试这个模式:
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())));
我收到了一组国家代码作为道具,如下所示:['en', 'ro', 'da']
。在这样的对象中将其映射回他们的名字的最佳方法是什么:{value: 'en', label: 'English}
.
试试这个模式:
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())));