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
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.
for (var key in obj) { subArr.push(obj[key]) }.