Created
March 30, 2017 10:22
-
-
Save shamsher31/cc645cac15e1a074250eff9bb04e722f to your computer and use it in GitHub Desktop.
Flatten Nested Array of Objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example http://stackoverflow.com/questions/30916031/lodash-deepflatten-array-of-objects | |
function getPeople(persons){ | |
var result = []; | |
_.each(persons, function(person){ | |
result.push({name: person.name, age: person.age}); | |
person.children && (result = _.union(result,getPeople(person.children))) | |
}); | |
return result | |
} | |
////// | |
[ | |
{ | |
"name": "George", | |
"age": 45, | |
"children": [ | |
{ | |
"name": "Chris", | |
"age": 38, | |
"children": [ | |
{ | |
"name": "Nick", | |
"age": 35, | |
"children": [ | |
{ | |
"name": "Maria", | |
"age": 63 | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment