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

133
Vistas
Ignore TypeError during For Loop

I'm trying to figure out how to "ignore" TypeErrors during a For Loop, but I would like to pass an empty string value to my variable when I do come across a TypeError or a property that does not exist. I have tried some try/catch with TypeError, but I'm not quite sure what to do with the catch. Any help is appreciated...

My obj

theObj = [ 
    {
        name:"Billy",
        age:"30",
        level:"10"
    },
    {
        name:"Jimbo",
        age:"25",
        level:"8"
    },
    {
        name:"Ralph",
        age:"37"
    },
    {
        name:"Rita",
        age:"23",
        level:"3"
    }
]

Loop

for (var i = 0; i < theObj.length; i++) {
    const levels = theObj[i]['level']
    console.log(levels)
}

console

10
8
undefined
3

What I would like to see

10
8

3

OR

10
8
Null or some other value like DNE, N/A
3
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

No problem

for (var i = 0; i < theObj.length; i++) {
    const levels = theObj[i]['level']
    console.log(levels ? levels : "")
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try theObj[i]?.level, like:

const theObj = [ 
    {
        name:"Billy",
        age:"30",
        level:"10"
    },
    {
        name:"Jimbo",
        age:"25",
        level:"8"
    },
    {
        name:"Ralph",
        age:"37"
    },
    {
        name:"Rita",
        age:"23",
        level:"3"
    }
];

for (let i = 0; i < theObj.length; i++) {
    const levels = theObj[i]?.level;
    console.log(levels)
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

you should try this

const levels = theObj?.[i]?.['level'] || ""

?. make sure that the parent is defined and if not it will return undefined

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