Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  44] [ 7]  / answers: 1 / hits: 29796  / 13 Years ago, mon, january 9, 2012, 12:00:00

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

More From » json

 Answers
22

Here's a shorter solution:



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

[#88144] Saturday, January 7, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;