Skip to content

Instantly share code, notes, and snippets.

@kikill95
Created November 8, 2016 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikill95/6f89f64c4795c15607aada2a89019852 to your computer and use it in GitHub Desktop.
Save kikill95/6f89f64c4795c15607aada2a89019852 to your computer and use it in GitHub Desktop.
Recursion in JavaScript
var tree = {
father: 'papa_0', mother: 'mama_0', children: [
{
father: 'papa_1_0', mother: 'mama_1_0', children: [
{
father: 'papa_2_0', mother: 'mama_2_0', children: [
{
father: 'papa_3_0', mother: 'mama_3_0', children: []
},
{
father: 'papa_3_1', mother: 'mama_3_1', children: []
},
{
father: 'papa_3_2', mother: 'mama_3_2', children: []
}
]
}
]
},
{
father: 'papa_1_1', mother: 'mama_1_1', children: []
}
]
};
function getAllChildren(family) {
if (family) {
console.log('Parents: ', family.father, family.mother);
}
if (family.children.length) {
family.children.forEach(getAllChildren);
}
}
getAllChildren(tree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment