Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
Keyword search of object returning array

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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

When using Object.keys you only get the keys of the array. In your case just "0" and "1". 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')

about 4 years ago · Juan Pablo Isaza Report

0

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;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!