Son Nguyen
Son Nguyen

Reputation: 11

Empty array - Google apps script

I must get a value in a json data like in following link: Value in json data (sorry because I can't emb image). I need to get value of "DT sau CK" and I see that it's value is an empty array [ ]. My function is:

function test () {
  api_url = 'https://my url....'
  var result = UrlFetchApp.fetch(api_url);  
  var data = JSON.parse(result);
  var dtt = data['chart']['data'][2]['value'];
  if (dtt == false || dtt.length == 0) {
    dtt = 0;
  } else {
    dtt = data['chart']['data'][2]['value'];
  }
  return Number(dtt)
}
My if "dtt.length == 0" for an emty array. I want it to give me value/number 0 but it always return another number 157548000, something like that, maybe by api_url/server. But i want to get a number "0" result as i saw an empty array. Can anyone help me, thanks a lot and sorry for my bad English.

Upvotes: 0

Views: 4614

Answers (2)

Musaddiq
Musaddiq

Reputation: 39

I faced same issue recently, the solution is to just delete empty/extra columns.

Upvotes: 0

Riël
Riël

Reputation: 1321

Maybe you can add this to replace empty arrays with '0':

chart.data.forEach(function (item) {
 if (item.value.length===0) {
   item.value=0;
  }
});

Upvotes: 1

Related Questions