简体   繁体   中英

how to get array of array in javascript

I'm creating graph and for this I need data in this form in javascript:

var currYear = [["2011-08-01",796.01], ["2011-08-02",510.5], ["2011-08-03",527.8], ["2011-08-04",308.48]]

Data is coming from JSON

var total_click = JSON.parse(<%=raw @report.to_json %>);
var i=0;
var graph_type = "";
var graph_typ = new Array();
while(i < total_click.length){
graph_typ.push("['"+total_click[i].report_date+"',"+total_click[i].clicks+"]");
i++;
}

Data which I get from this is below

['2012-03-19',48],['2012-03-20',14],['2012-03-21',934]

How do I get this format

[['2012-03-19',48],['2012-03-20',14],['2012-03-21',934]]

Instead of creating a string, push the arrays into an array:

var total_click = JSON.parse(<%=raw @report.to_json %>);
var graph_type = [];
for(var i = 0; i < total_click.length; i++){
  graph_type.push([ total_click[i].report_date, total_click[i].clicks ]);
}
graph_typ.push("['"+total_click[i].report_date+"',"+total_click[i].clicks+"]");

is not the way you add an array into another one. Here you are building an array of strings.

while(i < total_click.length){
var arr = [];
arr.push(total_click[i].report_date);
arr.push(total_click[i].clicks);
graph_typ.push(arr);
i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM