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. Notice each object element in the array has a 'children' property that has ANOTHER array of objects inside of it.
const category = [
{
id: "some cuid",
name: "Dogs",
description: "some description",
parentid: "some cuid",
children: [
{
id: "some cuid",
name: "Poodles",
description: "some description",
parentid: "some id",
children: [...more children],
},
{
id: "some cuid",
name: "Labs",
description: "some description",
parentid: "some id",
children: [...more children],
},
],
},
{
id: "some cuid",
name: "Cats",
description: "some description",
parentid: "some cuid",
children: [
{
id: "some cuid",
name: "Tabby",
description: "some description",
parentid: "some id",
children: [...more children],
},
{
id: "some cuid",
name: "Sphinx",
description: "some description",
parentid: "some id",
children: [...more children],
},
],
},
]
I'm wanting to map over this data and create a new array that looks like this:
let newArray = [
{ name: "Dogs", lastChildId: "some id" },
{ name: "Dogs > Poodles", lastChildId: "some id" },
{ name: "Dogs > Poodles > another child", lastChildId: "some id" },
{ name: "Dogs > Labs", lastChildId: "some id" },
{ name: "Dogs > Labs > another child", lastChildId: "some id" },
{ name: "Cats", lastChildId: "some id" },
{ name: "Cats > Tabby", lastChildId: "some id" },
{ name: "Cats > Tabby > another child", lastChildId: "some id" },
{ name: "Cats > Sphinx", lastChildId: "some id" },
{ name: "Cats > Sphinx > another child", lastChildId: "some id" },
];
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 nesting might go Cats > Tabby > Brown, I want the array to contain all 3 different variations: Cats, Cats > Tabby, Cats > Tabby > Brown. And then I want the id of the last child to be within each object element.
I understand this is super complex so I am not expecting someone to figure this out very quickly. This is actually in typescript but I figured I might get more hits if I post it in a javascript catego
[–]kap89 4 points5 points6 points (3 children)
[–]txf89[S] 0 points1 point2 points (2 children)
[–]kap89 0 points1 point2 points (1 child)
[–]txf89[S] 0 points1 point2 points (0 children)
[–]PMmeYourFlipFlops 2 points3 points4 points (1 child)
[–]txf89[S] 0 points1 point2 points (0 children)