Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda