Skip to content

Instantly share code, notes, and snippets.

@tjeastmond
Created March 6, 2019 21:12
Show Gist options
  • Save tjeastmond/e61776a563964b9607d1c1102651ad57 to your computer and use it in GitHub Desktop.
Save tjeastmond/e61776a563964b9607d1c1102651ad57 to your computer and use it in GitHub Desktop.
Because I couldn't not do it...
function getNames(person) {
const names = [];
names.push(person.name);
person.children.forEach(child => names.push(...getNames(child)));
return names;
}
const people = {
name: "Robin",
children: [
{
name: "Alberto",
children: [
{
name: "Quinn",
children: [
{
name: "Conner",
children: []
},
{
name: "Lila",
children: []
}
]
}
]
},
{
name: "Charlie",
children: []
}
]
};
console.log("getNames(people): ", getNames(people));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment