javascript - 使用对象数组查找每个国家/地区的大陆

标签 javascript arrays typescript find

我有这个对象数组:

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,
  }
})

但我得到 Argument of type '(this: void, continent: { continent: string; countries: string[]; }) => boolean[]' is not assignable to parameter of type '(value: { continent: string; countries: string[]; }, index: number, obj: { continent: string; countries: string[]; }[]) => boolean'. 类型“boolean[]”不可分配给类型“boolean”

最佳答案

您的代码可以更高效、更简洁,方法是首先创建一张国家到大陆的 map 。这将为每个国家/地区删除所有需要的搜索和过滤,您只需要按国家/地区为您的 map 编制索引。

您可以使用 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)

关于javascript - 使用对象数组查找每个国家/地区的大陆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693346/

相关文章:

javascript - 尽管导入模块并包含 CUSTOM_ELEMENTS_SCHEMA 架构,但组件不是已知元素

javascript - javascript 数组条件的简写版本

javascript - Web 套接字 - 服务器负载

javascript - three.js 中线框正方形的无限平面

Javascript AJAX 函数返回类型未定义

arrays - Powershell添加成员计算值

c++ - 使用两个并行数组 C++

javascript - 先按字母排序,再按数字排序

javascript - 使用带有嵌套样式的样式组件用于 React 搜索栏,尝试将两个不同的 SVG 放置在搜索栏的相对两侧

javascript - 将变量从javascript传递到服务器(django)