skip to Main Content

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?

3

Answers


  1. 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 = ['US','Canada','Mexico','North Korea','Korea','Germany'];
    const second = ['North America','France','India','America'];
    
    function removeSimilar(conutries) {
      const result = [];
    
      conutries.forEach((country, i) => {
        const previousSlice = conutries.slice(0, i);
        const filtered = previousSlice.filter(el => el.includes(country));
        if(filtered.length == 0)
          result.push(country);
      });
      
      return result;
    }
    
    console.log(removeSimilar(first));   // ['US', 'Canada', 'Mexico', 'North Korea', 'Germany']
    console.log(removeSimilar(second));  // ['North America', 'France', 'India']
    
    Login or Signup to reply.
  2. So you want to filter() the array of countries and remove a country if some() other country includes() the country name.

    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 is there is to prevent a country from matching against itself with whole.includes(part).

    Login or Signup to reply.
  3. 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.

    const array1 = ['US','Canada','Mexico','North Korea','Korea','Germany'];
    const array2 = ['North America','France','India','America'];
    
    const removeFunc = (countries) => {
        return countries.reduce((acc, country) => {
          // shorter but maybe messier return option
          // return (acc.some(elem => elem.includes(country))) ? acc : [...acc, country];
          if(acc.some(elem => elem.includes(country))) return acc;
          return [...acc, country];
        }, [])
      }
      
    console.log(removeFunc(array1));
    console.log(removeFunc(array2));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search