Created
November 8, 2016 18:55
-
-
Save kikill95/6f89f64c4795c15607aada2a89019852 to your computer and use it in GitHub Desktop.
Recursion in JavaScript
This file contains 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
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