2014-04-11 117 views

回答

2
//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]); 
    }); 
}); 
+0

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

+0

您的意思是您想要改變數據嗎? – Borre

+0

Bore-是的,它應該反映在圖表上。 – user3438326

1
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]]) 
} 
+0

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