請考慮這個示例jsfiddle。在這個例子中我想了var數據陣列將兩個數據數組組合並修改爲一個數組
[4,3],
[4,4],
[6, 6],
[6, 7],
[7,8],
[7,9],
[8,10],
[8,11]
作爲
[4,3,4],
[6,6,7],
[7,8,9],
[8,10,11]
我想要的[X,Y]字段被修改以[X,Y1,Y2]進行修改。這個怎麼做 ?
請考慮這個示例jsfiddle。在這個例子中我想了var數據陣列將兩個數據數組組合並修改爲一個數組
[4,3],
[4,4],
[6, 6],
[6, 7],
[7,8],
[7,9],
[8,10],
[8,11]
作爲
[4,3,4],
[6,6,7],
[7,8,9],
[8,10,11]
我想要的[X,Y]字段被修改以[X,Y1,Y2]進行修改。這個怎麼做 ?
//Original data
var data = [[4,3],
[4,4],
[6, 6],
[6, 7],
[7,8],
[7,9],
[8,10],
[8,11]]
//Group data by x coordinate in an object
var intermediate = {};
for(var i = 0; i < data.length; i++){
intermediate [data[i][0]] = (intermediate [data[i][0]] || []).concat(data[i][1]);
}
//Convert intermediate object to array
var result = [];
for(var i in intermediate){
result.push([parseInt(i)].concat(intermediate[i]));
}
編輯:更改數據的其他方式:
var data = [[4,3,4],[6,6,7],[7,8,9],[8,10,11]];
var result = [];
data.forEach(function(row){
row.slice(1).forEach(function(cell){
result.push([row[0], cell]);
});
});
var result = [];
var arr = [[4,3], [4,4],[6, 6],[6, 7],[7,8],[7,9],[8,10],[8,11]];
for(var i = 0;i< arr.length;i+=2){
result.push([arr[i][0], arr[i][1], arr[i+1][1]])
}
Romko - 我要聲明爲[4,3,4], 的VAR值數據[6,6,7], [7,8,9], [8,10,11]而不是[4,3], [4,4], [6,6], [6 ,7], [7,8], [7,9], [8,10], [8,11]在這個例子中 – user3438326
Borre - 我想var數據值被聲明爲[4,3,4],[6,6,7],[7,8, 9],[8,10,11]而不是[4,3],[4,4],[6,6],[6,7],[7,8],[7,9],[8 ,10],[8,11] – user3438326
您的意思是您想要改變數據嗎? – Borre
Bore-是的,它應該反映在圖表上。 – user3438326