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.

6
  • 1
    A conversion to a javascript array or some other language? Commented May 18, 2013 at 5:51
  • +1 @Unipartisandev a javscript array. Commented May 18, 2013 at 5: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. Commented May 18, 2013 at 5: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. Commented May 18, 2013 at 5: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. Commented May 18, 2013 at 6:15

2 Answers 2

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Though you answered the question as asked, I rather doubt this form of data is really what the function wants.
@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
+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 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 :)
1

More succinctly (in ECMAscript 5):

function toArray (obj) {
  return Object.keys (obj).map (function (k) {
    var v = {}; v[k] = obj[k]; return v; 
  });
}

Comments

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.