2016-08-01 65 views
0

我有一個JavaScript數組這樣JavaScript的數組二維數組

var myArr = [ 

    { 
     "Year":"2015", 
     "Month":"January", 
     "Value":"15.8", 
     "District":"Anuradhapura", 
     "type":"Rainfall" 
    }, 
    { 
     "Year":"2015", 
     "Month":"January", 
     "Value":"31.1", 
     "District":"Anuradhapura", 
     "type":"Temparature" 
    }, 
    { 
     "Year":"2015", 
     "Month":"January", 
     "Value":"4", 
     "District":"Anuradhapura", 
     "type":"Wind" 
    }, 
    { 
     "Year":"2015", 
     "Month":"January", 
     "Value":"69", 
     "District":"Anuradhapura", 
     "type":"Humidity" 
    } 
] 

我需要的是把typeValue數據爲2維數組。我的最終結果應該是這樣的;

var data = [ 
     [ 
      "Rainfall", 
      158 
     ], 
     [ 
      "Temparature", 
      31.1 
     ], 
     [ 
      "Wind", 
      4 
     ], 
     [ 
      "Humidity", 
      69 
     ] 
    ] 

注意myArr結果我從後端服務,該數組的長度讓我可以動態改變。我怎樣才能做到這一點。感謝

回答

5

試試這個:

隨着foreach功能

文檔:Array.forEach

var data = []; 

myArr.forEach(x => data.push([x.type, parseFloat(x.Value)])) 

隨着map功能

文檔:Array.Map

var data = myArr.map(x => [x.type, parseFloat(x.Value)]); 
+0

雖然你將不得不增加仍轉換x.Value一個數值。 :) –

+0

@winner_joiner感謝 –

5

可以簡單地使用Array.map功能對象陣列映射到二維陣列。

var data = myArr.map(function(o){ 
     return [o.type, o.Value] 
    }); 

此外,如果你想要的值轉換成float數字代替string,與pasreFloat

var data = myArr.map(function(o){ 
     return [o.type, pasreFloat(o.Value)] 
    }); 
+1

爲了不被挑剔,但在他的數中的一個是浮動,所以你應該使用paraeFloat – Jay

+0

@Wade ..感謝您使用一個單獨的數組注意到 –

2

爲此,您可以使用Arrays.map函數來得到結果:

 var data = myArr.map(function(input){ return [input.type, input.Value]; }); 

這個函數會將數組中的每個元素轉換爲另一個元素,因爲你需要一個數組數組,你的mappin g函數必須從一個對象中創建一個數組。

1
let data = []; 
myArr.map(a => { 

data.push([a.type, a.Value]); 

}); 
+0

是沒有必要的,因爲地圖功能將「創造「它是自己的。 –

-1

我熟悉underscore.js:你可以使用:

_.zip(_.pluck(myArr, "type"), _.pluck(myArr, "Value")); 

var myArr = [ 
 

 
    { 
 
     "Year":"2015", 
 
     "Month":"January", 
 
     "Value":"15.8", 
 
     "District":"Anuradhapura", 
 
     "type":"Rainfall" 
 
    }, 
 
    { 
 
     "Year":"2015", 
 
     "Month":"January", 
 
     "Value":"31.1", 
 
     "District":"Anuradhapura", 
 
     "type":"Temparature" 
 
    }, 
 
    { 
 
     "Year":"2015", 
 
     "Month":"January", 
 
     "Value":"4", 
 
     "District":"Anuradhapura", 
 
     "type":"Wind" 
 
    }, 
 
    { 
 
     "Year":"2015", 
 
     "Month":"January", 
 
     "Value":"69", 
 
     "District":"Anuradhapura", 
 
     "type":"Humidity" 
 
    } 
 
]; 
 

 

 
alert(JSON.stringify(_.zip(_.pluck(myArr, "type"), _.pluck(myArr, "Value"))));
<script src="http://underscorejs.org/underscore-min.js"></script>

+2

如果函數存在於語言的核心中,那麼使用外部庫是過量的。 https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/map –

+0

下劃線是一個必須有庫,它有助於操縱JSON。功能的速度和效率都很快。並且重量非常輕。休息你的建議是明顯的 –

+1

您有一個觀點我,我只是觀察隔離的問題。順便說一句。大多數瀏覽器都支持JSON,請查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON。只是爲javascipt核心函數5.7kb與0 kb的爭論。 - > 0k更「輕量級」;-) –