javascript - How to fill array inm loop JS? -
i have following array:
var pageviews = [ [1, randvalue()], [2, randvalue()], [3, randvalue()], ];
this array filled staticly. how can fill in loop?
i tried:
$.each(data, function(k, v) { pageviews.push([num, v["value"]]); })
but gives me different result
please try:
var data = {a:"hii", b:"bbye"}, // dummy data pageviews = []; $.each(data, function(key,val){ var temp = []; temp.push(key,val) pageviews.push(temp); }); //output, pageviews = [["a","hii"],["b","bbye"]]
if want index number define separate counter variable as:
var data = {a:"hii", b:"bbye"}, // dummy data pageviews = [], counter = 1; $.each(data, function(key,val){ var temp = []; temp.push(counter++,val) pageviews.push(temp); }); //output, pageviews = [[1,"hii"],[2,"bbye"]]
Comments
Post a Comment