0

i have a javascript array like this

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. 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. how can i do this. thanks

5 Answers 5

5

Try this:

With foreach function

Documentation: Array.forEach

var data = [];

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

With map function

Documentation: Array.Map

var data = myArr.map(x => [x.type, parseFloat(x.Value)] );
Sign up to request clarification or add additional context in comments.

1 Comment

although you would have to add still convert the x.Value to an numeric value. :)
5

You can simply use Array.map function to map object array to two dimensional array.

  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

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

1 Comment

Not to be nitpicky, but in one of his the number is a float, so you should use paraeFloat
2

You can use Arrays.map function to get the result:

     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.

Comments

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

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

});

1 Comment

using a separate Array is not necessary since map-function would "create" it's own.
-1

I am familiar with underscore.js: you may use:

_.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>

3 Comments

Using an external library is overkill, if the function exists in the core of the language. developer.mozilla.org/de/docs/Web/JavaScript/Reference/…
underscore is a must have library and it helps to manipulate json. speed and efficiency of functions are fast. and very light in weight. rest your suggestion is appreciable
You have a point I, I just was viewing the problem isolated. btw. support of JSON has grown in the most browsers, checkout this link developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . And just to make a argument for the javascipt core functions 5.7kb vs 0 kb. -> 0k is more "light-weighter" ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.