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

232
Views
javascript: valor de matriz vacío

Tengo el siguiente ciclo que llama a una API y envía datos a una matriz, el problema es que la API devuelve valores vacíos para ciertos atributos, es decir "primaryTag": null y está rompiendo mi código, ¿cómo puedo manejarlo para colocar una estática? valor si alguno de los valores es nulo?

 for (var a in audience) { var aId = audience[a]; var url = base+'?'+query+'&AudienceId='+aId var req = new HttpClientRequest(url); req.header["Content-Type"] = "application/json" req.method = "GET" req.execute(); var resp = req.response; if( resp.code != 200 ) throw "HTTP request failed with " + resp.message var posts = JSON.parse(resp.body) logInfo(resp.code+' '+url); for (i = 0; i < 11; i++) { articlesList_json.push({ "title":posts[i].title, "pubDate":posts[i].publishedDate, "link":posts[i].url, "imageURL":posts[i].imageUrl, "description": posts[i].description, "category": posts[i].category.name, "audience": posts[i].audience.name+'-'+posts[i].audience.id, "tag": posts[i].primaryTag.name, "episerverId":posts[i].episerverId, }); } }//for loop end

ingrese la descripción de la imagen aquí

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Eche un vistazo al encadenamiento opcional

 "tag": posts[i].primaryTag?.name ?? "N/A"

Alternativamente, use un ternario:

 "tag": posts[i].primaryTag ? posts[i].primaryTag.name : "N/A"
about 4 years ago · Juan Pablo Isaza Report

0

Valores como cadenas nulas, indefinidas, vacías, 0 y NaN evaluados como falsos en Javascript.

Puede verificar si el valor no es nulo con una condición if

 if(value) { // value is not null // do something with the value }

En su ejemplo, puede usar una expresión ternaria para verificar esto en su lugar

 for (i = 0; i < 11; i++) { articlesList_json.push({ // check if value is not falsy and provide a default when it is "title":posts[i].title ? posts[i].title : "no value", // ... }); }

consulte https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator para obtener más información

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!