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

229
Vistas
API weather undefined. How do I create an IF statment in App Scripts

I downloaded a weather API to get the value of the current conditions, which all works well except for the alert "event" and "description".

If an "alert" exists I get the value, but when there is none, the result is "TypeError: Cannot read property '0' of undefined".

What I need is an IF statement that says "if undefined, "0" else get the value" for these two lines:

const alerts = resJSON["alerts"][0]["event"] 
const alertDs = resJSON["alerts"][0]["description"]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could use optional chaining

const resJSON = {alerts: [{notevent: "test", description: "my description"}]};
const alerts = resJSON?.alerts?.[0]?.event || "0";
const alertDs = resJSON?.alerts?.[0]?.description || "0";
console.log(alerts);
console.log(alertDs);

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