Looking to transform a JSON into a tree structure of objects. Consider the following example:
{
"id": 2,
"label": "BEAUTY",
"description": "",
"parent_id": 0,
},
{
"id": 5,
"label": "SunGlass",
"description": "",
"parent_id": 2,
},
{
"id": 6,
"label": "Shirts",
"description": "",
"parent_id": 2,
},
{
"id": 41,
"label": "black Glasses",
"description": "electronique",
"parent_id": 5,
},
{
"id": 34,
"label": "T-shirts",
"description": "electronique",
"parent_id": 6,
},
{
"id": 3,
"label": "Phones",
"description": "",
"parent_id": 0,
"embedded_parent": null,
}
The goal is to convert this list into a tree object based on the 'label' attribute. The expected result looks like this:
const TREE_DATA = {
BEAUTY: {
'SunGlass': {'black Glasses':null},
'Shhirts': null,
},
Phones: {
'Sbardilate': null,
'T-shirrrrsees': null,
'Balons': null,
},
};
A recursive function is needed to achieve this result, in order to pass it to my Angular component widget efficiently. Thank you for your help!