我有这个对象数组:
const CONTINENT_MAP = [
{
continent: 'Oceania',
countries: ['Australia', 'Fiji'],
},
{
continent: 'Europe',
countries: [
'Anguilla',
'Aruba',
'Austria',
'Azerbaijan',
'Belgium',
'Bulgaria',
'Croatia',
'Curacao',
'Cyprus',
'Denmark',
'Finland',
'France',
'Georgia',
'Germany',
'Greece',
'Italy',
'Luxembourg',
'Malta',
'Monaco',
'Netherlands',
'Netherlands Antilles',
'Norway',
'Poland',
'Portugal',
'Romania',
'Spain',
'Sweden',
'Switzerland',
'Ukraine',
'United Kingdom',
],
},
{
continent: 'Asia',
countries: [
'Bahrain',
'China',
'East Timor',
'India',
'Indonesia',
'Iran',
'Iraq',
'Israel',
'Japan',
'Jordan',
'Kazakhstan',
'Myanmar',
'North Korea',
'South Korea',
'Kuwait',
'Kyrgyzstan',
'Laos',
'Malaysia',
'Oman',
'Pakistan',
'Qatar',
'Russian Federation',
'Saudi Arabia',
'Singapore',
'Taiwan',
'Thailand',
'Turkey',
'Turkmenistan',
'United Arab Emirates',
'Uzbekistan',
'Vietnam',
'Yemen',
],
},...]
另一个数组:
const dataset = [{country: 'Vietnam', color: 'blue'}, {country: 'Matla', color: 'red'}, ...]
我想循环数据集并获取每个国家的大陆。国家/地区字符串之间的比较必须不区分大小写。 所以我写了这个函数:
export function equalsStringIgnoreCase(str1: string, str2: string) {
return typeof str1 === 'string' && typeof str2 === 'string'
? str1.localeCompare(str2, undefined, { sensitivity: 'base' }) === 0
: str1 === str2
}
然后,我尝试这段代码:
const result = dataset.map(value => {
const continent = CONTINENT_MAP.find(cont => {
return cont.countries.map(country => {
return equalsStringIgnoreCase(country, value.country)
})
// return continent.countries.includes(value.country) // easy but case sensitive
})
return {
continent: continent.continent,
country: value.country,
}
})
但是我得到类型为'(this:void,continental:{continent:string; country:string [];})=> boolean []'的参数不能分配给类型为'(value {大陆:字符串;国家:字符串[];},索引:数字,obj:{大陆:字符串;国家:字符串[];} [])=>布尔值”。 不能将类型“ boolean []”分配给类型“ boolean” 。
答案 0 :(得分:1)
通过首先创建一个国家/地区到大陆的地图,您的代码可以更高效,更简洁。这将删除每个国家/地区所需的所有搜索和过滤功能,您只需要按国家/地区对地图进行索引即可。
您可以使用reduce
来做到这一点,这是获得所需结果的代码:
const countriesToContinent = CONTINENT_MAP.reduce((acc, { continent, countries }) => {
countries.forEach(country => acc[country.toLocaleLowerCase()] = continent);
return acc;
}, {});
const dataset = [{country: 'Vietnam', color: 'blue'}, {country: 'Matla', color: 'red'}];
const result = dataset.map(({ country, color }) => ({
continent: countriesToContinent[country.toLocaleLowerCase()],
country,
}));
还有一个实时示例:
const CONTINENT_MAP = [
{
continent: 'Oceania',
countries: ['Australia', 'Fiji'],
},
{
continent: 'Europe',
countries: [
'Anguilla',
'Aruba',
'Austria',
'Azerbaijan',
'Belgium',
'Bulgaria',
'Croatia',
'Curacao',
'Cyprus',
'Denmark',
'Finland',
'France',
'Georgia',
'Germany',
'Greece',
'Italy',
'Luxembourg',
'Malta',
'Monaco',
'Netherlands',
'Netherlands Antilles',
'Norway',
'Poland',
'Portugal',
'Romania',
'Spain',
'Sweden',
'Switzerland',
'Ukraine',
'United Kingdom',
],
},
{
continent: 'Asia',
countries: [
'Bahrain',
'China',
'East Timor',
'India',
'Indonesia',
'Iran',
'Iraq',
'Israel',
'Japan',
'Jordan',
'Kazakhstan',
'Myanmar',
'North Korea',
'South Korea',
'Kuwait',
'Kyrgyzstan',
'Laos',
'Malaysia',
'Oman',
'Pakistan',
'Qatar',
'Russian Federation',
'Saudi Arabia',
'Singapore',
'Taiwan',
'Thailand',
'Turkey',
'Turkmenistan',
'United Arab Emirates',
'Uzbekistan',
'Vietnam',
'Yemen',
],
}
];
const countriesToContinent = CONTINENT_MAP.reduce((acc, { continent, countries }) => {
countries.forEach(country => acc[country.toLocaleLowerCase()] = continent);
return acc;
}, {});
const dataset = [{country: 'Vietnam', color: 'blue'}, {country: 'Austria', color: 'red'}];
const result = dataset.map(({ country, color }) => ({
continent: countriesToContinent[country.toLocaleLowerCase()],
country,
}));
console.log(result)