Last active
May 25, 2022 04:25
-
-
Save chihebnabil/e05e186a593cabadb3deebd6869687f5 to your computer and use it in GitHub Desktop.
javascript : flat array to tree
This file contains 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
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