Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Creating a new array from recursive mapping

tdev81

New Coder
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:

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.
 

New Threads

Buy us a coffee!

Back
Top Bottom