Nested Array Recursive
This Nest Array Recursive
Function is used to
Flatten the Nested Array Objects and Filter the undefined routeUrls.
Type Declaration
menuTypes.ts
export type subMenus = {
menuId: string;
routeUrl: string;
subMenus: subMenus[];
}
export type MenuProps = {
menuId: string;
routeUrl: string;
subMenus: subMenus[];
}[];
Filter Recursive Menus
filterRecursiveMenu.ts
export function FilterNestedArray(menus: MenuProps){
let flattendedMenus: subMenus[] = [];
function flattenRecursiveMenus(m: any){
for (let i of m){
flattendedMenus.push(i);
if(i.subMenus?.length){
flattenRecursiveMenus(i.subMenus)
}
}
}
flattenRecursiveMenus(menus)
return flattendedMenus?.filter((m) => m.routeUrl !== undefined);
}