Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Randy_Johnson's avatar

Recursive Forever Menu!

Hi I am currently trying to create a recursive forever menu, I need to read through the menu in such away that no matter the depth of a object, eg. object.link > object.links > object.link it can read into.

My final result is to create a drop down side menu.

const [menu, setMenu] = useState([
        {
            name: "Customers",
            desc: "A description of the side bar.",
            src: "icons8-collect-64.png",
            open: false,
            links: [
                {
                    name: 'Students',
                    url: 'www.google.com',
					links: [
{
						name: 'create',
						url: 'some url',
}
]
                },
                {
                    name: 'Parents',
                    url: 'www.google.com',
                },
            ]
        },
        {
            name: "Creativity",
            desc: "A description of the side bar.",
            src: "icons8-collect-64.png",
            open: false,
            links: [
                {
                    name: 'Projects',
                    url: '/projects',
                    open: false,
                    links: [
                        {
                            name: 'Project 1',
                            url: '',
                        },
                        {
                            name: 'Project 2',
                            url: '',
                        },
                        {
                            name: 'Project 3',
                            url: '',
                        },
                    ],
                },
                {
                    name: 'Pictures',
                    url: '/pictures',
                },
                {
                    name: 'Poems',
                    url: '/poems',
                },
                {
                    name: 'Stories',
                    url: '/stories',
                },
            ]
        },
    ]);
0 likes
3 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

Just my opinion, it would be good to have a set max depth. Makes looping over it easier.

Also see https://gist.github.com/jimgwhit/cbbe5bb0d2556fdc7e37a86d3630239c

Concerning looping. Without a known max depth it just makes it harder, though there are flatten routines you can modify for this as needed.

During the flatten you could store the depth.

Also, and just me, looping over arrays is easier.

Edit

When I have Iterating to do, I just work it out normally with some practice and trial and error.

Please or to participate in this conversation.