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

200
Visualizações
Vanilla Javascript filter function works perfect in one line, but not work at all for other

I am trying to filter JSON file with plain JavaScript.

city = await cities.filter(ea => {
    return ea.city.toLowerCase().includes(searcher.value.toLowerCase()); //example value Belgrade
});

admin = await cities.filter(eb => {
    return eb.admin_name.toLowerCase().includes(searcher.value.toLowerCase()); //example value Beograd
});

While filter that goes to city variable works perfect, one that goes to admin variable, witch is Copy-Paste of first one, return error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'toLowerCase')

and even if I remove toLowerCase function I get same error but (reading 'includes')

this is structure of JSON file:

[
 {
  "city": "Tokyo",
  "city_ascii": "Tokyo",
  "lat": 35.6839,
  "lng": 139.7744,
  "country": "Japan",
  "iso2": "JP",
  "iso3": "JPN",
  "admin_name": "Tōkyō",
  "capital": "primary",
  "population": 39105000,
  "id": 1392685764
 },
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That just means admin_name doesn't exist, or is null/undefined for at least one object of your array.

In order to support this, you could use optional chaining like this:

admin = cities.filter(eb => {
    return eb.admin_name?.toLowerCase()?.includes(searcher.value.toLowerCase()); //example value Beograd
});

As a side note, awaiting a non Promise value just returns that value, so it's useless in your case.

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