Given an array of regions, where the highest region has key: 10 and parent_id: null, the goal is to restructure this array in order to return a tree representation.
The desired regions tree structure for input [10] should be:
- Egypt
- Zone 1
- Tagamo3
- Giza
- Helwan
- Fayoum
- Zone 2
- Gesr ElSuis
- test
- Delta
- Mohandeseen
- Down Town
- Gesr ElSuis
Array:
[
{
"key": 1,
"title": "Zone 1",
"parent_id": 10
},
{
"key": 2,
"title": "Zone 2",
"parent_id": 10
},
...
]
The desired output for input [10]:
[
{
"key": 10,
"title": "Egypt",
"parent_id": null,
"children": [
{
"key": 1,
"title": "Zone 1",
"parent_id": 10,
"children": [
...
]
},
{
"key": 2,
"title": "Zone 2",
"parent_id": 10,
"children": [
...
]
}
]
}
]
...