Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert object key-value pairs to a series of arrays in Javascript [duplicate]

I am new to Javascript. I have a Javascript object like so:

s = {"Toothless":"Dragon","Foo":"Bar"};

I need to convert it into a series of arrays, like so:

out = [["Toothless","Dragon"],["Foo","Bar"]];

This is the reverse of what is discussed in Convert JavaScript array of 2 element arrays into object key value pairs. A JQuery solution is acceptable.

like image 572
Akshat Mahajan Avatar asked Dec 05 '22 02:12

Akshat Mahajan


1 Answers

You can map over the items to achieve this:

    s = {"Toothless":"Dragon","Foo":"Bar"};
    var out = Object.keys(s).map(function(data){
        return [data,s[data]];
    });
    console.log(out);
like image 177
baao Avatar answered Dec 09 '22 14:12

baao