0

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.

1
  • you can not filter a nested array, because you need to either mutate the children or get only the parents filtered. please add the wanted result. Commented May 17, 2021 at 18:04

1 Answer 1

1

Here is some code to demonstrate a solution:

let 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' },
      ]},
  ];

function filter(item) {
  if (['Apps', 'Steve'].includes(item.label) ) { // keep it?
    if (item.items) {
      item.items = item.items.filter(filter) // apply filter to subarray
    }
    console.log(item)
    return true
  }
  return false // else filter it out
}

model = model.filter(filter)

As a demonstration this will keep only "Apps" with only "Steve" in its subarray.

Sign up to request clarification or add additional context in comments.

1 Comment

Not quite what I need. I need to filter all the model nodes and their sub-nodes using the same filtering scheme, but retain the model structure for the nodes that remain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.