简体   繁体   English

javascript数组到二维数组

[英]javascript array to two dimension array

i have a javascript array like this 我有一个像这样的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"
   }
]

what i need is to put type and Value data into 2 dimension array. 我需要将typeValue数据放入二维数组。 my end result should be like this; 我的最终结果应该是这样;

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

note that myArr results i'm getting from the backend service and the length of this array can be change dynamically. 请注意,我从后端服务获得的myArr结果可以动态更改此数组的长度。 how can i do this. 我怎样才能做到这一点。 thanks 谢谢

Try this: 尝试这个:

With foreach function 具有foreach功能

Documentation: Array.forEach 文档: Array.forEach

var data = [];

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

With map function 具有map功能

Documentation: Array.Map 文档: Array.Map

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

You can simply use Array.map function to map object array to two dimensional array. 您可以简单地使用Array.map函数将对象数组映射到二维数组。

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

Also if you want value to be converted into a float number instead of string , do this with pasreFloat 另外,如果要将值转换为float而不是string ,请使用pasreFloat进行此pasreFloat

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

You can use Arrays.map function to get the result: 您可以使用Arrays.map函数获取结果:

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

This function will transform each element of your array into another element, because you need an array of arrays, your mapping function must create an array out of an object. 此函数会将数组的每个元素转换为另一个元素,因为您需要一个数组数组,因此映射函数必须根据对象创建一个数组。

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

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

});

I am familiar with underscore.js: you may use: 我熟悉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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM