我正在创建图表,为此我需要在javascript中使用此表单中的数据:
var currYear = [["2011-08-01",796.01], ["2011-08-02",510.5], ["2011-08-03",527.8], ["2011-08-04",308.48]]
数据来自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++;
}
我从中获得的数据位于
之下['2012-03-19',48],['2012-03-20',14],['2012-03-21',934]
如何获取此格式
[['2012-03-19',48],['2012-03-20',14],['2012-03-21',934]]
答案 0 :(得分:4)
不是创建字符串,而是将数组推送到数组中:
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 ]);
}
答案 1 :(得分:3)
graph_typ.push("['"+total_click[i].report_date+"',"+total_click[i].clicks+"]");
不是将数组添加到另一个数组的方式。在这里,您将构建一个字符串数组。
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++;
}