如何删除包含在其他数组元素中的元素。

huangapple go评论58阅读模式
英文:

How to remove element that are coming inside other array elements

问题

Here's the translated code part without the explanation:

// 国家数组(输入)
['US','Canada','Mexico','North Korea','Korea','Germany'];
// 期望的输出
['US','Canada','Mexico','North Korea','Germany'];

// 国家数组(输入)
['North America','France','India','America'];
// 期望的输出
['North America','France','India'];

你可以尝试使用以下方法来获得期望的输出:

// 示例代码
const countries = ['US','Canada','Mexico','North Korea','Korea','Germany'];
const filteredCountries = countries.filter(country => !['Korea', 'America'].includes(country));
// 现在filteredCountries包含了期望的输出
英文:

I have an array of countries coming from the database.

Here are 2 examples of the input, and output I'm expecting:

// countries array (input)
['US','Canada','Mexico','North Korea','Korea','Germany'];
// expected output
['US','Canada','Mexico','North Korea','Germany']; 

// countries array (input)
['North America','France','India','America'];
// expected output
['North America','France','India'];

I want to remove Korea and America from the respective arrays as I already have North Korea and North America corresponding to those elements.

I have tried includes() method but it's not working as it checks for the exact value.

How can I get the expected output?

答案1

得分: 1

以下是代码的翻译部分:

filter()数组中的国家,并在其他国家includes()国家名称时删除国家。

function solution(countries) {
  return countries.filter((part) => (
    !countries.some(whole => whole !== part && whole.includes(part))
  ));
}

console.log(solution(['US','Canada','Mexico','North Korea','Korea','Germany']));
console.log(solution(['North America','France','India','America']));

whole !== part是为了防止国家与自己匹配时使用whole.includes(part)

英文:

So you want to filter() the array of countries and remove a country if some() other country includes() the country name.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function solution(countries) {
  return countries.filter((part) =&gt; (
    !countries.some(whole =&gt; whole !== part &amp;&amp; whole.includes(part))
  ));
}

console.log(solution([&#39;US&#39;,&#39;Canada&#39;,&#39;Mexico&#39;,&#39;North Korea&#39;,&#39;Korea&#39;,&#39;Germany&#39;]));
console.log(solution([&#39;North America&#39;,&#39;France&#39;,&#39;India&#39;,&#39;America&#39;]));

<!-- end snippet -->

whole !== part is there is to prevent a country from matching against itself with whole.includes(part).

答案2

得分: 0

以下是翻译好的部分:

如果后一个元素在前一个元素中,则应将其移除。是吗?您可以使用 slice() 获取前面元素的数组,并使用 filter() 检查每个元素是否包含特定字符串,就像这样。

const first = ['美国', '加拿大', '墨西哥', '朝鲜', '韩国', '德国'];
const second = ['北美洲', '法国', '印度', '美国'];

function removeSimilar(countries) {
  const result = [];

  countries.forEach((country, i) => {
    const previousSlice = countries.slice(0, i);
    const filtered = previousSlice.filter(el => el.includes(country));
    if (filtered.length == 0)
      result.push(country);
  });

  return result;
}

console.log(removeSimilar(first));   // ['美国', '加拿大', '墨西哥', '朝鲜', '德国']
console.log(removeSimilar(second));  // ['北美洲', '法国', '印度']
英文:

Well, if later element is 'included' in previous element's', then it should be removed. Is it right? You can get an array of previous elements with slice() and check whether each element includes certain string with filter() like this.

const first = [&#39;US&#39;,&#39;Canada&#39;,&#39;Mexico&#39;,&#39;North Korea&#39;,&#39;Korea&#39;,&#39;Germany&#39;];
const second = [&#39;North America&#39;,&#39;France&#39;,&#39;India&#39;,&#39;America&#39;];

function removeSimilar(conutries) {
  const result = [];

  conutries.forEach((country, i) =&gt; {
    const previousSlice = conutries.slice(0, i);
    const filtered = previousSlice.filter(el =&gt; el.includes(country));
    if(filtered.length == 0)
      result.push(country);
  });
  
  return result;
}

console.log(removeSimilar(first));   //&#160;[&#39;US&#39;, &#39;Canada&#39;, &#39;Mexico&#39;, &#39;North Korea&#39;, &#39;Germany&#39;]
console.log(removeSimilar(second));  // [&#39;North America&#39;, &#39;France&#39;, &#39;India&#39;]

答案3

得分: 0

我们可以使用reduce函数来执行此操作,其中我们检查其他元素是否包含该字符串。这假设更具体的字符串后面出现在数组中,即"America"会出现在"North America"之后。

const array1 = ['US', 'Canada', 'Mexico', 'North Korea', 'Korea', 'Germany'];
const array2 = ['North America', 'France', 'India', 'America'];

const removeFunc = (countries) => {
    return countries.reduce((acc, country) => {
        if (acc.some(elem => elem.includes(country))) return acc;
        return [...acc, country];
    }, [])
}

console.log(removeFunc(array1));
console.log(removeFunc(array2));
英文:

We can just use a reduce to perform that action where we check if other element includes the string. This assuming that the more especific comes later in the array, ie America will appear after north america.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const array1 = [&#39;US&#39;,&#39;Canada&#39;,&#39;Mexico&#39;,&#39;North Korea&#39;,&#39;Korea&#39;,&#39;Germany&#39;];
const array2 = [&#39;North America&#39;,&#39;France&#39;,&#39;India&#39;,&#39;America&#39;];

const removeFunc = (countries) =&gt; {
    return countries.reduce((acc, country) =&gt; {
      // shorter but maybe messier return option
      // return (acc.some(elem =&gt; elem.includes(country))) ? acc : [...acc, country];
      if(acc.some(elem =&gt; elem.includes(country))) return acc;
      return [...acc, country];
    }, [])
  }
  
console.log(removeFunc(array1));
console.log(removeFunc(array2));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 15:47:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定