I am attempting to do something very complex.
I have a database that has a self relation (categories that contain a nested category).
The ORM I’m using makes use of these categories as objects:
I’m wanting to output a new array of objects that look like this:
Basically I want to output a name of the complete path through the nested category names, but also outputting the intermediate steps between nesting (ie. even though one category might go Cats > Brown > Short, I want the array to contain all 3 different variations: Cats, Cats > Brown, Cats > Brown > Short.
This is going to be output in a drop down select list.
I understand this is super complex so I am not expecting someone to figure this out very quickly.
I have a database that has a self relation (categories that contain a nested category).
The ORM I’m using makes use of these categories as objects:
Code:
category = [
{
name: "Lights",
description: "some description",
parentid: "some id",
children: [
[{name: "bulbs", description: "some description", parentid: "some id", children: [...more children]}]
[{name: "LEDs", description: "some description", parentid: "some id", children: [...more children]}]
[{name: "CFL", description: "some description", parentid: "some id", children: [...more children]}]
]
},
{
name: "Cats",
description: "some description",
parentid: "some id",
children: [
[{name: "brown", description: "some description", parentid: "some id", children: [...more children]}]
[{name: "white", description: "some description", parentid: "some id", children: [...more children]}]
[{name: "gray", description: "some description", parentid: "some id", children: [...more children]}]
]
},
]
I’m wanting to output a new array of objects that look like this:
Code:
newArray = [
{name: "Lights", lastChildId: "someid"},
{name: "Lights > bulbs", lastChildId: "someid"},
{name: "Lights > bulbs > *more children names...*", lastChildId: "someid"},
{name: "Lights > LEDs", lastChildId: "someid" },
{name: "Lights > LEDs > *more children names...*", lastChildId: "someid" },
{name: "Lights > CFL", lastChildId: "someid"},
{name: "Lights > CFL > *more children names...*", lastChildId: "someid"},
{name: "Cats", lastChildId: "someid"},
{name: "Cats > brown > *more children names...*", lastChildId: "someid"},
{name: "Cats > white > *more children names...*", lastChildId: "someid" },
{name: "Cats > gray > *more children names...*", lastChildId: "someid"},
]
Basically I want to output a name of the complete path through the nested category names, but also outputting the intermediate steps between nesting (ie. even though one category might go Cats > Brown > Short, I want the array to contain all 3 different variations: Cats, Cats > Brown, Cats > Brown > Short.
This is going to be output in a drop down select list.
I understand this is super complex so I am not expecting someone to figure this out very quickly.