Convert object to array with underscore
2 answers
You can use _.map
and _.values
like this
var data = [{ key1: 1, key2: 2 }, { key1: 3, key2: 4 }];
console.log(_.map(data, _.values));
# [ [ 1, 2 ], [ 3, 4 ] ]
If you want a generic JavaScript version, you can do
console.log(data.map(function(currentObject) {
return Object.keys(currentObject).map(function(currentKey) {
return currentObject[currentKey];
})
}));
# [ [ 1, 2 ], [ 3, 4 ] ]
+3
source to share