I'm facing difficulty in traversing through a nested array which may contain arrays of itself, representing a dynamic menu structure. Below is how the objects are structured:
This is the Interface IMenuNode:
Interface IMenuNode:
export interface IMenuNode {
title: string;
haveChildren: boolean;
id: string;
node: Array<IMenuNode>;
link: string;
img: string;
value: string;
}
Class DataNode implementing IMenuNode
export class DataNode implements IMenuNode {
title: string;
haveChildren: boolean;
id: string;
node: Array<IMenuNode>;
link: string;
img: string;
value: string;
userMenu: Array<IMenuNode>;
I have some data stored in MenuData as shown below:
const MenuData: Array<IMenuNode> =
[
new DataNode('Menu 1', true, 'menu1', [
new DataNode('SubMenu 1', true, 'submenu1',[
new DataNode('SubSubMenu1', false ,'subsubmenu1', null, "/", "pathSelectorIcon.png"),
new DataNode('SubSubmenu2', false, 'subsubmenu2', null ,"/", "pathSelectorIcon.png"),
]),
new DataNode('Menu 2', true, 'menu2', [
new DataNode('SubMenu 1', true, 'submenu1',[
new DataNode('SubSubMenu1', false ,'subsubmenu1', null, "/", "pathSelectorIcon.png"),
new DataNode('SubSubmenu2', false, 'subsubmenu2', null ,"/", "pathSelectorIcon.png"),
]),
How can I iterate over the entire MenuData (including recursively) and dynamically construct a new menu (userMenu) based on certain conditions to determine the items (menus and submenus) that should be included?