Creating a new array from recursive mapping

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:

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:

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 out this very quickly. Totally willing to venmo some coffee money if anyone is up for this javascript challenge (okay technically I am doing this with typescript but I think javascript will suffice).

Is the parentid on your top level items (Lights, Cats) null? or some specific value that identifies all of htem as being top level nodes?

Is there any way to edit my question? I don’t see an edit or delete button or anything.

You can only edit a post for the first 4 hours after posting it. You can find the delete button inside the ellipsis (…)

Ok, i am at the iPad and I had 5 beer and 3 grappa but I will try to give you a hint how to solve this

You need a recursive method to add the child’s.

function addChild(itemList, path, parent)
{
for(let child of parent.children)
{
const newItem = path + " → " + children.name;
ItemList.push(newItem);
addChild(itemList, newItem, child);
}
}
itemList = [category.name];
addChild(itemList, path, category);
console.log(itemList);

P.S.

I still do not know how to format a code correct on my iPad. Spaces are ignored

you’d need to foreach the top level of category because its an array of objects, but otherwise a solid dive for that deep into the drinking :wink:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.