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

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

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