Last active
October 27, 2017 13:07
-
-
Save marekdano/86e6f9917261504891db880032ef5dd2 to your computer and use it in GitHub Desktop.
example of looping recursively through array of objects
This file contains hidden or 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
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