Any suggestions on how to get nested arrays / objects from the following array of objects?
[
{COLOR: "Black", FABRIC_DESIGN: "Leather", SIZE: "S"}
{COLOR: "Black", FABRIC_DESIGN: "Leather", SIZE: "L"}
{COLOR: "Black", FABRIC_DESIGN: "Velvet", SIZE: "S"}
{COLOR: "Black", FABRIC_DESIGN: "Velvet", SIZE: "M"}
{COLOR: "Red", FABRIC_DESIGN: "Velvet", SIZE: "S"}
]
What I am trying to get is:
[
{
label: "Black",
children: [
{ label: "Leather", children: [{ label: "S" }, { label: "L" }] },
{ label: "Velvet", children: [{ label: "S" }, { label: "M" }] }
]
},
{
label: "Red",
children: [{ label: "Velvet", children: [{ label: "S" }] }]
}
];
I managed to do it for objects with 2 properties but not more than that, and I cant figure out how to do it for objects with N properties.
function groupBy(array, key) { /* ... */ return groupedArray; }. If this works then just use this same function for thechildrens of the groupedlabels