我有一个从AJAX函数返回的JSON数组。该数组如下所示:
{
"date":[
[
"2015-05-29",
"2015-05-12",
"2015-04-30",
"2015-03-30",
"2015-02-27",
"2015-02-26"
]
],
"close":[
[0,3,1,1,0,0]
],
"high":[
[1,3,2,1,0,1]
],
"low":[
[0,-1,0,-1,-1,-1]
]
}
我需要把'date'和'close'项目混合成一个看起来像这样的东西,对于JQPlot
var line1=[['2008-06-30 8:00AM',4], ['2008-7-30 8:00AM',6.5], ['2008-8-30 8:00AM',5.7], ['2008-9-30 8:00AM',9], ['2008-10-30 8:00AM',8.2]];
我想我需要循环这样的东西,但我不确定如何将值加入上面的新日期格式?
s3 = obj["close"][0];
dates = obj["date"][0];
for( var i = 0; i < dates.length; i++ ){
//not sure what to put here
}
答案 0 :(得分:1)
// the data object you get back from your ajax
var data = {"date":[["2015-05-29","2015-05-12","2015-04-30","2015-03-30","2015-02-27","2015-02-26"]],"close":[[0,3,1,1,0,0]],"high":[[1,3,2,1,0,1]],"low":[[0,-1,0,-1,-1,-1]]}
var time = " 8:00AM"
var dates = data.date[0]
var s3 = data.close[0]
// make sure the data is valid
// (`dates` and `s3` must have the same length in order to join them)
if (dates.length !== s3.length) {
throw new Error('oh no! the data is invalid')
}
var line1 = []
for(var i = 0; i < dates.length; i++) {
line1.push([dates[i] + time, s3[i]])
}
答案 1 :(得分:0)
你快到了。 在循环之前创建一个新数组。
var result = new Array;
然后在循环内部,您将在每次迭代时向该数组添加一个新项目。
result.push([dates[i], s3[i]]);
现在你的结果变量应该保存你想要的数组。