I have implemented a functionality to add multiple groups by searching from a list of group arrays. Each group has roles assigned to it (refer to the listOfgroupsWithRoles array).
- During the initial search, all the groups are returned in the array. If a group with an "Admin" role is selected and added, on subsequent searches to add a second group, the "Admin" role needs to be filtered out to only display other roles like "Support." To achieve this, I am using the filters method to exclude the roles that are already "Admin."
For example:
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;
}
During the first search, all array elements are displayed. From the second search onward, the filter method is used. To apply the filter, two for loops are utilized since there is an array of roles to compare. However, the output is currently an empty array.
this.filteredGroup = this.listOfgroupsWithRoles().filter(x => {
for (let j=0;j<=role.length;j++){ //role is from the first 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
}
}
});
- On the first search, all groups are displayed but when a group like "Server group 1" with an admin role is added. Expected outcome 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']
}
];
Upon subsequent searches, as the group with the "Admin" role was added previously, all groups without the admin role should be excluded or filtered out. Expected outcome below
{
id: 3,
name: 'Server group 3',
role: ['Support']
}
--- Despite implementing these steps, I am getting a blank array as the output. Can someone point out where I might be going wrong?