Seeking assistance with constructing a hierarchical tree structure from a flat list that contains categories and names. Various approaches have been attempted, including the function presented below.
The original flat list looks as follows:
var input = [
{
"Parent Category": "Agricultural Feed + Seed",
Category: "Agricultural Feed + Seed",
Name: "Agfinity"
},
...
];
The desired tree structure based on the flat list is outlined below:
[
{
"label": "Agricultural Feed + Seed",
"children": [
{
"label": "Agfinfity"
},
...
]
},
...
];
An existing function resembling the one provided attempts to achieve this conversion but may not be optimized.
function formatBrandNames(rawBrands) {
// Function code here
}
Any advice or suggestions on improving the efficiency of this process would be greatly valued.