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

140
Vistas
Uncaught TypeError: Cannot read properties of undefined even when using typeof- better approach?

My loop checks if an array of cards has a special attribute, if so it does a certain action.

  if(typeof cardArray[i]['special']!=='undefined'){
    // do stuff
}

Unfortunately, even though I'm using typeof to check the array contains a defined special key I'm still getting

Uncaught TypeError: Cannot read properties of undefined (reading 'special')

What am I doing wrong here or is there a better approach for this in general?

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

0

As mentioned in the comments, your problem is probably inside your loop.
Take a look at this example running without errors:

const cardArray = [{
  'special': 'pasta'
  }, {
  'regular': 'bread'
}]

for (let i = 0; i < cardArray.length; i++) {
  console.log(`Loop number ${i}`)
  if(typeof cardArray[i]['special']!=='undefined'){
    console.log(`Found: ${cardArray[i]['special']}`)
  }
}

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