Categories
javascript json underscore.js

How to use underscore.js to produce a flatten result

The json object is

var data = [{"Parent":1,"Child":[4,5,6]},{"Parent":2},{"Parent":3}]

How can I use underscore.js chain/map/pluck etc… function to get the flatten result

     var result = [];
for (var i = 0; i < data.length; i++) {
result.push(data[i].Parent);
if (data.Child != undefined) {
for (var j = 0; j < data[i].Child.length; j++) {
result.push(data[i].Child[j]);
}
}
}
console.log(result) >> //1,4,5,6,2,3

Here’s a shorter solution:

flat = _.flatten(_.map(data, _.values))