Home > Enterprise >  Keyword search of object returning array
Keyword search of object returning array

Time:06-07

Beginning coder here taking a js course. I'm almost done with a higher order function lesson, but am stuck. I have an object containing 250 countries with different info. Example:

const countries = [
    {
      name: 'Afghanistan',
      capital: 'Kabul',
      languages: ['Pashto', 'Uzbek', 'Turkmen'],
      population: 27657145,
      flag: 
'https://restcountries.eu/data/afg.svg',
      currency: 'Afghan afghani'
    },
    {
      name: 'Åland Islands',
      capital: 'Mariehamn',
      languages: ['Swedish'],
      population: 28875,
      flag: 
'https://restcountries.eu/data/ala.svg',
      currency: 'Euro'
    }, etc.

I'm asked to write a function that searches each country's name and returns an array of only the countries meeting the keyword criteria. I'm stumped and feel very lost. Here's what I have:

const keys = Object.keys(countries)
function categorizeCountries(keyword) {
    for (let i = 0; i < keys.length; i  ) {
        let country = countries.name
        if (country.includes(keyword)) {
            console.log(countries.filter((country) 
=> country.includes(keyword)))
     } else {
        console.log('Country not found')
        }
    }
    return country
}
categorizeCountries('land')
scategorizeCountries('stan')

I'm sure the issue is in my conditional statement, but I don't know how else to go about this. Any help is greatly appreciated.

CodePudding user response:

When using Object.keys you only get the keys of the array. In your case just 1, 2. What you want is the values. And then it's quite easy to sort using the filter prototype.

const countries = [
    {
      name: 'Afghanistan',
      capital: 'Kabul',
      languages: ['Pashto', 'Uzbek', 'Turkmen'],
      population: 27657145,
      flag: 
'https://restcountries.eu/data/afg.svg',
      currency: 'Afghan afghani'
    },
    {
      name: 'Åland Islands',
      capital: 'Mariehamn',
      languages: ['Swedish'],
      population: 28875,
      flag: 
'https://restcountries.eu/data/ala.svg',
      currency: 'Euro'
    }];

const countriesArray = Object.values(countries);
function categorizeCountries(keyword) {
    
    console.log(countriesArray.filter(country => country.name.toLowerCase().includes(keyword.toLowerCase())))
}
categorizeCountries('af')

CodePudding user response:

This is a short snippet demonstrating how to do it with .filter() and .includes() (@IvanaMurray made a good point regarding the application of .toLowerCase() for the strings to be compared, which I have left out here for simplicity):

const countries = [
{
  name: 'Afghanistan',
  capital: 'Kabul',
  languages: ['Pashto', 'Uzbek', 'Turkmen'],
  population: 27657145,
  flag: 
'https://restcountries.eu/data/afg.svg',
  currency: 'Afghan afghani'
},
{
  name: 'Åland Islands',
  capital: 'Mariehamn',
  languages: ['Swedish'],
  population: 28875,
  flag: 
'https://restcountries.eu/data/ala.svg',
  currency: 'Euro'
}];

["land","stan","an"].forEach(s=>
 console.log(countries.filter(c=>
  c.name.includes(s)))
)

CodePudding user response:

you are looping incorrectly through countries (you don't need Object.keys and you omitted usage of i also you have to accamulate your result in array)

function categorizeCountries(keyword) {
    const result = [];
    for (let i = 0; i < countries.length; i  ) {
        let { name } = countries[i];
        if (name.includes(keyword)) {
            console.log(name);
            result.push(name);
     } else {
        console.log('Country not found')
     }
    }
    return result;
}
  • Related