2

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 a multidimesional 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
  • Have you tried: api.jquery.com/jQuery.map Commented May 18, 2013 at 6:21
  • 1
    note that the order of key1..key4 is not defined in the original array. Commented May 18, 2013 at 7:02
  • @Floradu88 I don't quite understand how this could be a duplicate question. I'm asking how I'd go about converting from an object map, not an array. Commented May 19, 2013 at 17:01
  • @dk123 removed the comment. Commented May 19, 2013 at 17:04
  • @Floradu88 Thanks. It would have been nice if those other people that voted for this thread to be closed to have at least left an explanation; Commented May 19, 2013 at 17:07

3 Answers 3

5

Try this function:

function convert(original) {
    var multiArray = [];
    for(var key in original) { multiArray.push([ key, original[key] ]); }
    return multiArray;
}

See demo fiddle.

Use it like this:

var myObject = {
    "key1" : { data: "data1", extra: "none" },
    "key2" : { data: "data2", extra: "none" },
    "key3" : { data: "data3", extra: "none" },
    "key4" : { data: "data4", extra: "none" }, 
};
var myMultiArray = convert(myObject);
Sign up to request clarification or add additional context in comments.

7 Comments

You could just use for (var i in original), since for ... in is for object keys.
@JanDvorak you are right, I overlooked it. Just improved. Thanks!
Better now :-) Note that you're still assuming that noone has extended Object.prototype (but that's a safe assumption to make).
@JanDvorak Curious. In the current code? Where am I making that assumption, exactly?
@acdcjunior when you do for(i in x), it iterates over all enumerable properties of x, including those in the prototype chain. When you do Object.prototype.bind = function(){...}, every object has an enumerable property called bind, that this loop will return. When you do it, you break jQuery, too.
|
3

If you make a helper function pair like below, then the standard keys/map is good enough:

Object.keys(m).map(pair.bind(m))
function pair(x){return [x, this[x]]}

12 Comments

elegant and succinct.
I'm not that familiar with JS; I don't quite understand how I'd go about using this. Could you perhaps provide an example?
@dk123 not replaces, evaluates to. Ex: output = Object.keys(input).map(pair.bind(input)). Note the second line is supposed to be somewhere in the header
@dk123 bind won't work in old IE unless polyfilled. acdcjunior's answer will work everywhere.
|
3

In ES6 it's as easy as [[e, o[e]] for (e in o)].

4 Comments

+1 Thanks, this really looks amazing. I chose geocar's answer in the end due to current lack of general support for ES6. Great answer though, it really makes me look forward to the capabilities of what's coming!
@neil This fails in 2017 (chromium/firefox). Are you sure it‘s going to work like this?
@undko Sadly it was only a proposal and didn't make ES6 in the end. The latest proposal is [...Object.entries(o)] which I think works in current Firefox/Chrome.
@neil Thanks! A bit more verbose, but still a big improvement compared to loops :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.