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

209
Vistas
TypeError: Cannot read properties of undefined (reading 'endsWith')

I am trying to print out all countries which end with 'land' from the countries array below:

const countries = [
  'Albania',
  'Bolivia',
  'Canada',
  'Denmark',
  'Ethiopia',
  'Finland',
  'Germany',
  'Hungary',
  'Ireland',
  'Japan',
  'Kenya'
]

newArr = [];

for (let i = 0; i <= countries.length; i++){
  console.log(countries[i])
  if (countries[i].endsWith('land') === true){
    newArr.push(countries[i])
  }
  else{
    continue
  }
}

However, I am having a TypeError: Cannot read properties of undefined (reading 'endsWith)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

out of index try this

for (let i = 0; i <= countries.length - 1; i++) {
    console.log(countries[i]);
    if (countries[i].endsWith("land") === true) {
        newArr.push(countries[i]);
    } else {
        continue;
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Don't use = because index will be one less than the array length!

const countries = [
  'Albania',
  'Bolivia',
  'Canada',
  'Denmark',
  'Ethiopia',
  'Finland',
  'Germany',
  'Hungary',
  'Ireland',
  'Japan',
  'Kenya'
]

newArr = [];

for (let i = 0; i < countries.length; i++){
  console.log(countries[i])
  if (countries[i].endsWith('land') === true){
    newArr.push(countries[i])
  }
  else{
    continue
  }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Hope you have already got the answer from comments and other answers, but here is few improvement you can do using array reduce and &&

const countries = [
  'Albania',
  'Bolivia',
  'Canada',
  'Denmark',
  'Ethiopia',
  'Finland',
  'Germany',
  'Hungary',
  'Ireland',
  'Japan',
  'Kenya'
]

const z = countries.reduce((acc, curr) => {
  curr.endsWith('land') && acc.push(curr);
  return acc;
}, []);
console.log(z)

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