I have the model below and I need to filter the main array and items subarray by role when one is present.
this.model = [
{label: 'Home', icon: 'pi pi-fw pi-home', routerLink: ['/main']},
{label: 'Apps', icon:'pi pi pi-fw pi-desktop', routerLink: ['/main/admin'], role: 's_user',
items: [
{label: 'Steve', icon: 'pi pi-fw pi-desktop', url: ['ExtURLPlaceholder'], role: 's_user' },
{label: 'Roger', icon: 'pi pi-fw pi-desktop', url: ['ExtURLPlaceholder'] },
]},
{label: 'Admin', icon:'pi pi pi-fw pi-list', routerLink: ['/main/admin'], role: 'y_admin',
items: [
{label: 'Susbsystems', icon: 'pi pi-fw pi-id-card', routerLink: ['/main/admin/subsystems']},
{label: 'Logs', icon: 'pi pi-fw pi-bookmark', routerLink: ['/uikit/floatlabel'], role: 's_user' },
]},
];
I'm able to filter the top level array like this:
this.model = this.model.filter((item)=>{
if(item.role){
const token = this.oauthService.getAccessToken();
return this.jwtService.isAuthorized(token, [item.role])
}
return true
})
How can I also filter the items subarray in the same way?
The desired result would return all the items and subitems that match the role requirement if they have a role defined, and filter out those that don't if they have a role defined.