2015-05-29 28 views
1

我試圖調整highchart以迎合我的需求。我想使像的陣列(如下所述)

categories: ['2015-04-07', '2015-08-07', '2015-12-07', '2015-16-07', '2015-20-07', '2015-24-07', 2015-28-07', '2015-22-07', '2015-26-07', '2015-30-07', '2015-34-07', '2015-38-07'] 

和我有JSON東西,解析獲取的數據eqaul到每個數據從上方。

$.each(JSON.parse(response.var), function(index, value){       
//value.creation_date.replace('00:00:00', '').trim(); 
}); 

對於每個value.creation_date,內容等(2015年5月14日00:00:00),所以我不得不使用

replace('00:00:00', '').trim(); 

使得結果將是「2015 - 05-14" 。如何把每個

value.creation_date.replace('00:00:00', '').trim() 

像一個數組,使它看起來像這樣

categories: ['2015-04-07', '2015-08-07', '2015-12-07', '2015-16-07', '2015-20-07', '2015-24-07', 2015-28-07', '2015-22-07', '2015-26-07', '2015-30-07', '2015-34-07', '2015-38-07'] 

我想聲明一個全局變量像

var num = ""; 

,然後$。每個

num = + "," + value.creation_date.replace('00:00:00', '').trim(); 

並試圖顯示它看它是否工作

$("body").html(num); 

但可悲的是不工作。任何想法,線索,建議,建議?

回答

1

下面的代碼應該讓你的數組:

var num = [];  
$.each(JSON.parse(response.var), function(index, value){       
    num.push(value.creation_date.replace('00:00:00', '').trim()) ; 
}); 
1

像這樣的東西應該這樣做,最初看起來像這樣

[{ 
    creation_date: '2015-05-14 00:00:00' 
}, { 
    creation_date: '2015-05-13 00:00:00' 
}, { 
    creation_date: '2015-05-12 00:00:00' 
}] 

會像現在這樣

var data = JSON.parse(response.var); 
$.each(data, function(index, value){ 
    data[index].creation_date = data[index].creation_date.replace('00:00:00', '').trim(); 
}); 

數據

[{ 
    creation_date: '2015-05-14' 
}, { 
    creation_date: '2015-05-13' 
}, { 
    creation_date: '2015-05-12' 
}] 

或者你可以只使用javascript .forEach()像這樣

var data = JSON.parse(response.var); 
data.forEach(function(value, index){ 
    data[index].creation_date = data[index].creation_date.replace('00:00:00', '').trim(); 
}); 
1

如果我正確理解你的問題,這裏是我的解決方案

var categories = []; 
$.each(JSON.parse(response.var), function(index, value){       
    categories.push(value.creation_date.replace('00:00:00', '').trim()); 
}); 
console.log(categories); 
1

像一個數組,使它看起來像這樣

categories: ['2015-04-07', '2015-08-07', '2015-12-07', '2015-16-07', '2015-20-07', '2015-24-07', 2015-28-07', '2015-22-07', '2015-26-07', '2015-30-07', '2015-34-07', '2015-38-07'] 

我的理解是,你正在試圖形成一個數組,將其作爲輸入提供給Highchart。

正如你提到的,你不喜歡的東西,

num = + "," + value.creation_date.replace('00:00:00', '').trim(); 

爲什麼?你需要的是一個數組。運行這個fiddle並查看控制檯。

var xAxis = {} 
xAxis.categories = []; 
xAxis.categories.push('2015-04-07'); 
xAxis.categories.push('2015-04-08'); 
xAxis.categories.push('2015-04-09'); 
xAxis.categories.push('2015-04-10'); 
console.log(xAxis); 
console.log(xAxis.categories); 

我建議你去通過JavaScript ObjectArray概念一旦..