Skip to content
Advertisement

Recreate an object from an array of objects in Javascript

I could use some help with the following:

I’m trying to create a filterable wordpress nav menu using React Tree Menu.

The Tree Menu component requires the following data structure:

JavaScript

I have set up parent and child pages of the above in wordpress and have also created a hierarchical wordpress menu. I am using the wordpress menu api

which returns the following data structure:

JavaScript

Can someone please help me turn what I receive from the wordpress menu api into what the Tree Menu component expects?

Thanks in advance for any help. -Ian

Advertisement

Answer

I’d use a recursive function to build the object you want. Something like the following:

JavaScript

Call that function on your source array and it should work. There’s a running example below if you want to try it out.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement