Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda