Skip to content

Instantly share code, notes, and snippets.

@marekdano
Last active October 27, 2017 13:07
Show Gist options
  • Save marekdano/86e6f9917261504891db880032ef5dd2 to your computer and use it in GitHub Desktop.
Save marekdano/86e6f9917261504891db880032ef5dd2 to your computer and use it in GitHub Desktop.
example of looping recursively through array of objects
const list = [
{
n: 1,
children: [{
n: 11,
children: []
}, {
n: 12,
children: []
}]
},
{
n: 2,
children: [{
n: 21,
children: [{
n: 211,
children: []
},
{
n: 212,
children: [{
n: 2111,
children: []
}]
}]
}]
},
{
n: 3,
children: []
}
];
function loop(list) {
list.forEach(item => {
console.log(item.n);
if (item.children.length > 0) {
loop(item.children);
}
})
}
loop(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment