Home > Net >  restcountries api map with filter
restcountries api map with filter

Time:09-17

I am trying to get the name of country by its border name.

const countryList = [
  { name: 'France', cca3: 'FRA' },
  { name: 'Austria', cca3: 'AUT' },
  { name: 'Belgium', cca3: 'BEL' }
  { name: 'Brazil', cca3: 'BRA' }
];

const country = [
  {
    name: {
      common: 'Germany'
    }    
  },
  {cca3: "DEU"},
  { borders: ['AUT', 'BEL', 'FRA']}
]

I loop over the "borders" of the "country" e.g. Germany. I need to get the name of the borders from the "countryList" by its "cca3".

So for Germany, the borders should be: France, Austria, Belgium

This is what i am trying to do, no my strong side.. can't get it work.

const getCountryBordersName = () => {
  const borderName = () => {
    country[0].borders.map((item) => countryList.find((code) => code.item = code).name)
  }
  return borderName;

  
}
console.log(getCountryBordersName());

CodePudding user response:

First of all, borderName is a function here. I'm not sure why you have a function inside a function, but in any case, to actually return the value you want, you should do return borderName(); or just skip it entirely and have the code in the main getCountryBordersName function.

Secondly, you are trying to get borders of country[0] which is the part containing name. What you want to do is get borders of country[2] (country[2].borders.map...)

Finally you're not comparing the right things (or anything) in the find function. = is for assigning values, whereas == is for comparing, and you are comparing the whole country object in countryList to .item which doesn't even exist.

You want to do .find((c) => c.cca3 == item).

So in total, it should be:

const borderName = () => {
  country[2].borders.map((border) => countryList.find((c) => c.cca3 == border).name);
}
console.log(borderName());

CodePudding user response:

  • using Array#reduce, iterate over countryList while updating a Map where cca3 is the key and name is the value
  • Using Array#map, iterate over borders to get the list of country names from the above map

const getCountryBordersName = (country, countryList) => {
  const { borders = [] } = country[2] ?? {};
  if(borders.length === 0) return [];
  const countryMap = countryList.reduce((map, { name, cca3 }) => 
    map.set(cca3, name)
  , new Map);
  return borders.map(border => countryMap.get(border));
}

const 
  countryList = [ { name: 'France', cca3: 'FRA' }, { name: 'Austria', cca3: 'AUT' }, { name: 'Belgium', cca3: 'BEL' }, { name: 'Brazil', cca3: 'BRA' } ],
  country = [
    { name: { common: 'Germany' } },
    { cca3: "DEU" },
    { borders: ['AUT', 'BEL', 'FRA'] }
  ];
console.log( getCountryBordersName(country, countryList) );

CodePudding user response:

You may filter the countryList array and get those matching the values found in the borders array of the country.

Here's a live demo that return an array containing the names of the countries that are borders to the supplied country:

const countryList = [{
      name: 'France',
      cca3: 'FRA'
    },
    {
      name: 'Austria',
      cca3: 'AUT'
    },
    {
      name: 'Belgium',
      cca3: 'BEL'
    }, {
      name: 'Brazil',
      cca3: 'BRA'
    }
  ],
  country = [
    {name: {common: 'Germany'}},
    {cca3: "DEU"},
    {borders: ['AUT', 'BEL', 'FRA']}
  ],
  getBorderCountriesNames = country => countryList.reduce((a, c) => {
    country[2].borders.includes(c.cca3) && a.push(c.name);
    return a;
  }, []);

// print the result
console.log(getBorderCountriesNames(country))

  • Related