Can anyone help converting the following list of parent-child objects:
I have below array of objects, Need to convert it into parent child order. each 'Members' attribute in object may have 1 or n objects inside it. In 'Members' array the 1st object is parent of 2nd one and 2nd is parent of third object.
So in 1st Member 'Video' is parent of 'West' and 'West' is parent of 'India' and so on..
I have tried to loop through the elements one by one but could not reach the desired outcome.
Any help with the logic or code would be really helpful.
Input :
[
{
"Members": [
{
"Name": "Videos"
},
{
"Name": "West"
},
{
"Name": "India"
}
]
},
{
"Members": [
{
"Name": "Machinery"
},
{
"Name": "South"
},
{
"Name": "Australia"
}
]
},
{
"Members": [
{
"Name": "Electronics"
},
{
"Name": "Midwest"
},
{
"Name": "Arab"
}
]
},
{
"Members": [
{
"Name": "Machinery"
},
{
"Name": "West"
},
{
"Name": "India"
}
]
},
{
"Members": [
{
"Name": "Electronics"
},
{
"Name": "NorthEast"
},
{
"Name": "Japan"
}
]
},
{
"Members": [
{
"Name": "Videos"
},
{
"Name": "South"
},
{
"Name": "Australia"
}
]
},
{
"Members": [
{
"Name": "Videos"
},
{
"Name": "West"
},
{
"Name": "Japan"
}
]
}
]
Expected Output :
[
{
"name": "Videos",
"children": [
{
"name": "West",
"children": [
{
"name": "India",
"children": []
},
{
"name": "Japan",
"children": []
}
]
},
{
"name": "South",
"children": [
{
"name": "Australia",
"children": []
}
]
}
]
},
{
"name": "Machinery",
"children": [
{
"name": "South",
"children": [
{
"name": "Australia",
"children": []
}
]
},
{
"name": "West",
"children": [
{
"name": "India",
"children": []
}
]
}
]
},
{
"name": "Electronics",
"children": [
{
"name": "Midwest",
"children": [
{
"name": "Arab",
"children": []
}
]
},
{
"name": "NorthEast",
"children": [
{
"name": "Japan",
"children": []
}
]
}
]
}
]
```