我在JavaScript有這些陣列的響應創建4個維度數組:
與Ajax調用
var data = [
{ y: '2017-01', a: 50, b: 90, c:110},
{ y: '2017-02', a: 65, b: 75, c:120},
{ y: '2017-03', a: 50, b: 50, c:10},
{ y: '2017-04', a: 75, b: 60, c:170},
{ y: '2017-05', a: 80, b: 65, c:60},
{ y: '2017-06', a: 90, b: 70, c:30},
{ y: '2017-07', a: 100, b: 75, c:10},
{ y: '2017-08', a: 115, b: 75, c:0},
{ y: '2017-09', a: 120, b: 85, c:0}
]
我創建了一個Ajax調用,讓回JSON。
$.ajax({
type: "POST",
url: "php/get_incomes_months.php",
data: {year:current_year},
dataType: "json",
success: function(data) {
var data_array = [
{y:data[0]['date'], a:data[0]['validated'], b:data[0]['revenue'], c:data[0]['costes']},
{y:data[1]['date'], a:data[1]['validated'], b:data[1]['revenue'], c:data[1]['costes']}
];
fooDone(data_array); //after we have data, we pass it to fooDone
},
error: function(data){
}
});
這是JSON:
0 Object
validated "0"
revenue "50244.81"
costes "0"
date "2017-01"
1 Object
validated "44788.16"
revenue "30640.51"
costes "0"
date "2017-02"
2 Object
validated "68324.64"
revenue "80363.51"
costes "11072.73"
date "2017-03"
3 Object
validated "0"
revenue "50244.81"
costes "0"
date "2017-04"
4 Object
validated "2738"
revenue "103145.62"
costes "11"
date "2017-05"
5 Object
validated "510"
revenue "1459283.09"
costes "213"
date "2017-06"
6 Object
validated "24034.21"
revenue "50367.81"
costes "0"
date "2017-07"
7 Object
validated "24"
revenue "50244.81"
costes "0"
date "2017-08"
在這裏取得成功的功能,我有一個FOR內創建數組bucle,其實我做手工....
success: function(data) {
var data_array = [
{y:data[0]['date'], a:data[0]['validated'], b:data[0]['revenue'], c:data[0]['costes']},
{y:data[1]['date'], a:data[1]['validated'], b:data[1]['revenue'], c:data[1]['costes']}
];
請幫忙
只需循環遍歷所有json對象,並用data_array.push(your_object) – NeuTronas
將它們推送到您的數據數組中,可以指定更多請求嗎? @NeuTronas –
@SergioSuarez看看我的答案的第一個選擇。這就是他正在談論的。 –