ROX February 2016

divide json on 2 arrays

i try to divide json array:

 var json = [{"date":"23","count":"100"},{"date":"23","count":"100"}]
 var labels = [],data=[];
 for(var i in json){
      labels.push(json[i].date);
      data.push(json[i].count);
  }
  alert(labels);

but in alert is only , What is problem ? there is https://jsfiddle.net/90597hzy/

Answers


user2181397 February 2016

json does not have date & count key

Replaced with name & label. It is working as expected

var json = [{"name":"23","label":"100"},
               {"name":"23","label":"100"}]
             var labels = [],data=[];
             for(var i in json){
                  labels.push(json[i].name);
                  data.push(json[i].label);
              }
              console.log(labels);

JSFIDDLE


progonkpa February 2016

var array = [{"name":"23","label":"100"},{"name":"23","label":"100"}]
var labels = [],data=[];

for(var i = 0; i < array.length; i++){
  labels.push(array[i].label);
  data.push(array[i].name);
}

console.log(labels);
console.log(data);

Post Status

Asked in February 2016
Viewed 1,208 times
Voted 11
Answered 2 times

Search




Leave an answer