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

195
Views
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 answers
Answer question

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