Convert Object to Array angularjs

Problem

What is the best way to convert:

{
 0: 'a',
 1: 'b',
 2: 'c'
},
 {
 0: 'd',
 1: 'e',
 2: 'f'
}

to:

[ [a [b,c] ], [d [e,f] ] ]

where the 0 property of objects have the array of 1 and 2 objects? Is there a way to do that? Thanks

Problem courtesy of: Lee Lee

Solution

Try to use map method:

var data = [{
    0: 'a',
    1: 'b',
    2: 'c'
}, {
    0: 'd',
    1: 'e',
    2: 'f',
    3: 'g'
}];

var result = data.map(function (obj) {
    var subArr = Object.keys(obj).slice(1).map(function(key) {
        return obj[key];
    });
    return [obj[0], subArr];
});

alert(JSON.stringify(result, null, 4));

Above function use the element at index 0 as the first item of the nested arrays, and everything else in the subarray.

Solution courtesy of: dfsq

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.