Skip to content

Instantly share code, notes, and snippets.

@ZackFox
Last active August 1, 2017 14:54
Show Gist options
  • Save ZackFox/71014f96c2c9e1785eb9e9a7983fb968 to your computer and use it in GitHub Desktop.
Save ZackFox/71014f96c2c9e1785eb9e9a7983fb968 to your computer and use it in GitHub Desktop.
function makeTree(list, node) {
const tree = [];
list.forEach((item) => {
if (item.parent_id !== null) {
if(list[item.parent_id-1]["children"] === undefined){
list[item.parent_id-1].children = [];
}
list[item.parent_id-1].children.push(item);
} else {
tree.push(item);
}
});
console.log(tree);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment