1

Say for example I had an object map of the following:

{
    "key1" : { data: "data1", extra: "none" },
    "key2" : { data: "data2", extra: "none" },
    "key3" : { data: "data3", extra: "none" },
    "key4" : { data: "data4", extra: "none" }, 
};

Is there a convenient way to convert it to an array something like this:

[
    { "key1" : { data: "data1", extra: "none" }},
    { "key2" : { data: "data2", extra: "none" }},
    { "key3" : { data: "data3", extra: "none" }},
    { "key4" : { data: "data4", extra: "none" }}, 
];

I have a function that requires an array, yet the data I'm receiving from a 3rd party plugin is in object arrays. It would be nice if there was some simple way to get the conversion done between the two.

dk123
  • 18,684
  • 20
  • 70
  • 77
  • 1
    A conversion to a javascript array or some other language? – Ryan Mortensen May 18 '13 at 05:51
  • +1 @Unipartisandev a javscript array. – dk123 May 18 '13 at 05:53
  • Are you looking to create a multidimensional array like `["key1", { data: "data1", extra: "none" }]`? I noticed that you used `{ "key1", { data: "data1", extra: "none" }}`which I is invalid. – AnthonyS May 18 '13 at 05:57
  • Somehow, I rather doubt your function actually wants the form in your second example because that's kind of hard to do anything with. Yes, it's an array, but probably not the kind of array your function wants. It's an array of objects, each of which has no known property so the function would have to iterate over all properties in each object just to find out what is has. That is a poor way to design an argument to a function so either the function is designed poorly or you aren't understanding what it really wants. – jfriend00 May 18 '13 at 05:58
  • @jfriend00 Actually, I need both. I have two functions to choose from and the input for one of them is the question I asked and the other's the multidimensional array you're talking about. I'll ask another question for the multidimensional array. – dk123 May 18 '13 at 06:15
  • @jfriend00 New question on multidimensional array conversion: http://stackoverflow.com/questions/16621327/javascript-turning-object-map-into-multidimensional-array?noredirect=1#comment23898616_16621327 – dk123 May 18 '13 at 06:27

2 Answers2

4

Iterate over the properties in your object and push them to an array:

var myArray=[];
for (k in myObj) {
    if(myObj.hasOwnProperty(k)){
        var newObj = {};
        newObj[k] = myObj[k];
        myArray.push(newObj);
    }
}
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Though you answered the question as asked, I rather doubt this form of data is really what the function wants. – jfriend00 May 18 '13 at 06:01
  • @dk123 Do note that that answer relies on ES5 for `keys`, which isn't supported in IE8 and earlier. You can see a full compatibility table [here](http://kangax.github.io/es5-compat-table/) – Asad Saeeduddin May 19 '13 at 17:24
  • +1 Thanks for the link, I didn't know that. I've deleted my last comment and finalised this as my answer considering compatibility across major browsers. – dk123 May 19 '13 at 17:31
  • @dk123 No problem, do remember to check that site before you use any cool JS features that were recently added. By the way, you don't need to mention that you + or -1d something in your comment each time, voting is supposed to be anonymous :) – Asad Saeeduddin May 19 '13 at 17:35
1

More succinctly (in ECMAscript 5):

function toArray (obj) {
  return Object.keys (obj).map (function (k) {
    var v = {}; v[k] = obj[k]; return v; 
  });
}
HBP
  • 15,685
  • 6
  • 28
  • 34