2016-08-01 64 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更“轻量级”;-) –