I'm currently working with a list of menus and submenus:
[
{
"NUA_ID_Menu": 1,
"TXT_Nom_Menu": "Menu 1",
"Liste_Sous_Menus": [
{
"TXT_Nom_Menu": "SubMenu 1",
"Liste_Direction_Menu": [
{
"NUA_ID_Direction": 2,
"TXT_Nom_Direction": "Direction A"
},
{
"NUA_ID_Direction": 4,
"TXT_Nom_Direction": "Direction C"
}
]
},
{
"TXT_Nom_Menu": "SubMenu 2",
"Liste_Direction_Menu": [
{
"NUA_ID_Direction": 4,
"TXT_Nom_Direction": "Direction C"
}
]
},
{
"TXT_Nom_Menu": "SubMenu 3",
"Liste_Direction_Menu": [
{
"NUA_ID_Direction": 3,
"TXT_Nom_Direction": "Direction B"
}
]
}
]
},
{
"NUA_ID_Menu": 2,
"TXT_Nom_Menu": "Menu 2",
"Liste_Sous_Menus": [
{
"TXT_Nom_Menu": "SubMenu 4",
"Liste_Direction_Menu": [
{
"NUA_ID_Direction": 2,
"TXT_Nom_Direction": "Direction A"
},
{
"NUA_ID_Direction": 3,
"TXT_Nom_Direction": "Direction B"
}
]
}
]
}
]
I am interested in filtering menus that contain submenus with Direction C, for instance. The desired output would look like this:
[
{
"NUA_ID_Menu": 1,
"TXT_Nom_Menu": "Menu 1",
"Liste_Sous_Menus": [
{
"TXT_Nom_Menu": "SubMenu 1",
"Liste_Direction_Menu": [
{
"NUA_ID_Direction": 2,
"TXT_Nom_Direction": "Direction A"
},
{
"NUA_ID_Direction": 4,
"TXT_Nom_Direction": "Direction C"
}
]
},
{
"TXT_Nom_Menu": "SubMenu 2",
"Liste_Direction_Menu": [
{
"NUA_ID_Direction": 4,
"TXT_Nom_Direction": "Direction C"
}
]
}
]
}
]
Alternatively, I want to filter the menus with submenus that contain an array of directions.
My attempts so far:
this.mesMenus=res.filter(menu=>{
return menu.Liste_Sous_Menus.filter(ssmenu=>{
return ssmenu.Liste_Direction_Menu.filter(direction=>{
return direction.NUA_ID_Direction==2;
}).length>0;
}).length>0;
});
and also:
this.mesMenus = res.map(function(menu) {
menu.Liste_Sous_Menus = menu.Liste_Sous_Menus.map(function(ssmenu) {
ssmenu.Liste_Direction_Menu.filter(function(direction) {
return direction.NUA_ID_Direction==4;
});
return ssmenu;
});
return menu;
});
I have successfully filtered with one child (this method works)
const submenus = [ 6];
this.mesMenus =res.filter(d => d.Liste_Sous_Menus.every(c => submenus.includes(c.NUA_Id_Sous_Menu)));
However, I am struggling with filtering for grandchild elements. Could anyone provide assistance? Thank You!