Skip to content

Instantly share code, notes, and snippets.

@asci
Last active February 24, 2019 09:45
Show Gist options
  • Save asci/07ba38588cce51636928b58ac8290bd6 to your computer and use it in GitHub Desktop.
Save asci/07ba38588cce51636928b58ac8290bd6 to your computer and use it in GitHub Desktop.
tree functions
function walkTree(onEachChildren, childrenKey = "children") {
return function treeWalker(elem, level = 0) {
onEachChildren(elem, level);
if (elem[childrenKey]) {
elem[childrenKey].forEach(item => treeWalker(item, level + 1));
}
};
}
// how to use
const renderTree = walkTree((elem, level) => {
console.log(elem.name.padStart(level + elem.name.length, " "));
});
renderTree(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment