Skip to content

Instantly share code, notes, and snippets.

@shamsher31
Created March 30, 2017 10:22
Show Gist options
  • Save shamsher31/cc645cac15e1a074250eff9bb04e722f to your computer and use it in GitHub Desktop.
Save shamsher31/cc645cac15e1a074250eff9bb04e722f to your computer and use it in GitHub Desktop.
Flatten Nested Array of Objects.
// 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