I have an array like this.
var main_childs = [
{child_name: 1, childs:[2,4]},
{child_name: 2, childs:[3]},
{child_name: 3, childs:""},
{child_name: 4, childs:""}
]
main_child.childname 1 have childs 2 and 4 that exist in main_childs so if main_childs.child_name is a child of one of the child name it will output like this
final_childs = [
{
1: {
name: 1,
childs: [
{
name: 2,
childs: [
{
name: 3,
childs: []
},
],
name: 4,
childs:[]
}
]
},
}
]
I'm trying to solve it using this function
function recursive(main_childs){
main_childs.forEach(i=> {
if(i.childs.length>0){
recursive(i.childs)
}
})
Any help will be appreciated