Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert JS Object to Array

I need to convert a hash map

{      "fruit" : ["mango","orange"],     "veg"   : ["carrot"] }  

to

[    { "type" : "fruit" , "name" : ["mango","orange"] } ,   { "type" : "veg" ,   "name" : ["carrot"] }  ] 

how do I do that??

like image 637
Sam Avatar asked Apr 26 '12 15:04

Sam


People also ask

Can we convert object to array in JavaScript?

JavaScript Objects Convert object's values to array You can convert its values to an array by doing: var array = Object. keys(obj) . map(function(key) { return obj[key]; }); console.

How do you convert an object to an array of objects?

Use the Object. values() method to convert an object to an array of objects, e.g. const arr = Object. values(obj) .

How do you change an object to an array in react?

To update an object in an array in React state: Use the map() method to iterate over the array. On each iteration, check if a certain condition is met. Update the object that satisfies the condition and return all other objects as is.

How do you convert an object to an array of key value pairs in Java?

Method 1: In this method, we will use Object. keys() and map() to achieve this. Approach: By using Object. keys(), we are extracting keys from the Object then this key passed to map() function which maps the key and corresponding value as an array, as described in the below example.


2 Answers

You can do it like this (in a working snippet):

var input = {       "fruit" : ["mango","orange"],      "veg"   : ["carrot"]  }     var output = [], item;    for (var type in input) {      item = {};      item.type = type;      item.name = input[type];      output.push(item);  }    // display result  document.write(JSON.stringify(output));

Or, if you or someone else has been extending the Object prototype with enumerable properties (which I think is a bad practice personally), then you could use this to protect from that:

var input = {       "fruit" : ["mango","orange"],      "veg"   : ["carrot"]  }     var output = [], item;    for (var type in input) {      if (input.hasOwnProperty(type)) {          item = {};          item.type = type;          item.name = input[type];          output.push(item);      }  }    // display result  document.write(JSON.stringify(output));

And, using some more modern functionality:

var input = {       "fruit" : ["mango","orange"],      "veg"   : ["carrot"]  };    var output = Object.keys(input).map(function(key) {     return {type: key, name: input[key]};  });    // display the result  document.write(JSON.stringify(output));
like image 159
jfriend00 Avatar answered Oct 01 '22 19:10

jfriend00


In a browser that supports ES5 – or where you added a shim for it:

var stuff = {      "fruit" : ["mango","orange"],     "veg"   : ["carrot"] }  var array = Object.keys(stuff).map(function(key) {     return {"type" : key, "name" : stuff[key] } }) 

See: Object.keys, Array's map

Or, in the old fashion way:

var stuff = {      "fruit" : ["mango","orange"],     "veg"   : ["carrot"] }  var array = []  for (var key in stuff) {     if (stuff.hasOwnProperty(key)) {         array.push({"type" : key, "name" : stuff[key] })     } } 

Please notice that in both cases the array's value are shared because in JS the objects are passed by reference. So, for instance, stuff["fruit"] and array[0].name points to the same reference of the array ["mango", "orange"]. It means, if you change one of them, the other will be changed as well:

stuff["fruit"].push("apple"); alert(array[0].name); // "mango", "orange", "apple" 

To avoid that, you can use slice to have a one-level deep copy of your array. So in the code above, instead of:

"name" : stuff[key] 

you will have:

"name" : stuff[key].slice(0) 

Hope it helps.

like image 45
ZER0 Avatar answered Oct 01 '22 20:10

ZER0