gpt4 book ai didi

javascript - 如何在javascript中获取数组的数组

转载 作者:行者123 更新时间:2023-11-30 07:44:12 26 4
gpt4 key购买 nike

我正在创建图表,为此我需要 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]]

最佳答案

不是创建字符串,而是将数组插入数组:

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 ]);
}

关于javascript - 如何在javascript中获取数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com