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

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

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