I have an array of objects like this:
[
{
"id": 2,
"title": "LA COPA",
"parent_menu_id": null
},
{
"id": 3,
"title": "CALENDARIO",
"parent_menu_id": null
},
{
"id": 5,
"title": "Toeo",
"parent_menu_id": 2
},
{
"id": 6,
"title": "Nice",
"parent_menu_id": 2
}
]
This is the structure of a menu. The key "parent_menu_id" means that item id: 5 is child of item id:2.
This is the desired output:
[
{
"id": 2,
"title": "LA COPA",
"active": true,
"parent_menu_id": null,
"submenus":[
{
"id": 5,
"title": "Toeo",
"active": true,
"parent_menu_id": 2
},
{
"id": 6,
"title": "Nice",
"parent_menu_id": 2
}
]
},
{
"id": 3,
"title": "CALENDARIO",
"active": true,
"parent_menu_id": null
}
]
I know the algorithm:
- if pareny_menu_id is different than null search the key id == parent_menu_id.
