Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
javascript: empty array value

I have the following loop which calls an api and pushes data to an array, the issue is that the API is returning empty values for certain attributes i.e "primaryTag": null and is breaking my code, how can I handle it to place a static value if any of the values are null?

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

enter image description here

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Have a look at optional chaining

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

Alternatively use a ternary:

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

0

Values like null, undefined, empty Strings, 0 and NaN evaluated as false in Javascript.

You can check if the value is not null with an if condition

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

In your example u can use a ternary expression to check this in place

        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",
                // ...
            });
        } 

check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator for more information

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda