Skip to content

Instantly share code, notes, and snippets.

@chihebnabil
Last active May 25, 2022 04:25
Show Gist options
  • Save chihebnabil/e05e186a593cabadb3deebd6869687f5 to your computer and use it in GitHub Desktop.
Save chihebnabil/e05e186a593cabadb3deebd6869687f5 to your computer and use it in GitHub Desktop.
javascript : flat array to tree
var arr = [
{id: 1, title: 'hello', parent_id: 0},
{id: 2, title: 'hello', parent_id: 0},
{id: 3, title: 'hello', parent_id: 1},
{id: 4, title: 'hello', parent_id: 3},
{id: 5, title: 'hello', parent_id: 4},
{id: 6, title: 'hello', parent_id: 4},
{id: 7, title: 'hello', parent_id: 3},
{id: 8, title: 'hello', parent_id: 2}
];
function getNestedChildren(arr, parent_id) {
var out = []
for(var i in arr) {
if(arr[i].parent_id == parent_id) {
var children = getNestedChildren(arr, arr[i].id)
if(children.length) {
arr[i].children = children
}
out.push(arr[i])
}
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment