How to use filter method to filter array based on array field values in angular

618 Views Asked by At

I have a add functionality for adding mulitlple groups by searching from a list of group array . Each group has a roles(or multiple roles) assigned to it(see the listOfgroupsWithRoles array )

  1. On first search i will return all the array of groups ( now suppose i selected a group which as "Admin" role and add it )
  2. If a role added in the first search is an "Admin" role then on the next search to add a 2nd group i will have to filter out all the admin roles(since it is already added) and only display the other roles like "Support" in the search functionality . For this purpose i am using the filters method to filter out the roles which are not "Admin"

for example: my array is

func getGroupsWithRoles(){

listOfgroupsWithRoles= [{
    id: '1',
    group: 'Server group 1',
    role: ['Admin'],
  },
  {
    id: '2',
    group: 'Server group 2',
    role: ['Admin', 'Support']
  },
  {
    id: 3,
    name: 'Server group 3',
    role: ['Support']
  }
];

 return this.listOfgroupsWithRoles;
}


for the first search i display all the array elements and from the second search onwards i am using filter method for filtering i am using two for loops since we have an array of roles to compare, but i am getting empty array output.


this.filteredGroup =  this.listOfgroupsWithRoles().filter(x => {
                for (let j=0;j<=role.length;j++){    //role is from the the fisrt search ['Admin']
                    for (let i=0; i<=x.role.length; i++){ //x.role is from the  listOfgroupsWithRoles
                        x.role[i] !== role[j] //checking if both are not same
                       }  
                }       
            });

  • while searching for 1 time , i have to display all the groups and i added a group "Server group 1" which has admin role Expected output below
 [{
    id: '1',
    group: 'Server group 1',
    role: ['Admin'],
  },
  {
    id: '2',
    group: 'Server group 2',
    role: ['Admin', 'Support']
  },
  {
    id: 3,
    name: 'Server group 3',
    role: ['Support']
  }
];

while searching for 2nd time onward since the group with "Admin" was already added . I will have to exclude or filtered out all the group which doesn't have admin role. Expected output below

 {
    id: 3,
    name: 'Server group 3',
    role: ['Support']
  }

--- but i am getting blank array output. can someone tell me what i am doing wrong here ?

1

There are 1 best solutions below

0
anncb On

If I can suggest this :

Base on role = ["Admin"] and listOfgroupsWithRoles is your table.

listOfgroupsWithRoles.filter(x => {
        let roleExists = false;
        for (let j=0;j<role.length;j++){    //role is from the the first search ['Admin']
            for (let i=0; i<x.role.length; i++){ //x.role is from the  listOfgroupsWithRoles
    
                if(x.role[i] === role[j]){ //checking if almost one value in Role table is present
                    roleExists = true
                }
            }
        } 
    
        if(!roleExists) {
            return true;   
        }
    });