Home > Software design >  How can I filter an array based on values ​from other arrays?
How can I filter an array based on values ​from other arrays?

Time:09-29

I have a problem with filtering the data array.

I have three arrays:

permissions:[] - The array has a list of permissions, that is: permission_id,name. It contains all permissions. For example:

permissions:[{permission_id: 1, name: XXX}, {permission_id: 2, name: XXX}]

Another array of data is roles, which includes all roles:

roles:[{role_id: 1, role_name: XXX}, {role_id: 2, role_name: XXX}]

And the last table contains the links between the roles and their permissions, that is:

permissionsAndRoles:[{id: 1, role_id: 1,permission_id:1}, {id:2, role_id: 2, permissions_id: 2},{id:3, role_id: 1, permissions_id: 2}]

What I would like to accomplish is to get an array of permissions which, after filtering, will only contain permissions that a given role does not have. For example:

filteredArray(permissions,roles,permissionsAndRoles,role_id){
//input role_id=2
//output:permissions:[{permission_id: 1, name:XXX}]

}

What i have tried:

fileteredArray: function(){
      return this.permissions.filter(
        permission => this.permissionsAndRoles.some(
          obj => this.roles.some(role=>)
        )
      )

And here I am, because I don't know how well to put together the whole logic of this filtering.

CodePudding user response:

Actually you don't need roles array in this case

Live Demo

const permissions = [{ permission_id: 1, name: 'p-111' }, { permission_id: 2, name: 'p-222' }];
// const roles = [{ role_id: 1, role_name: 'r-111' }, { role_id: 2, role_name: 'r-222' }];
const permissionsAndRoles = [{ id: 1, role_id: 1, permission_id: 1 }, { id:2, role_id: 2, permissions_id: 2 },{ id:3, role_id: 1, permissions_id: 2 }];

const hasNotPermissionsByRole = (permissionsAndRoles, permissions, targetRoleId) => {
  const permissionsByRole = permissionsAndRoles
    .filter(({ role_id }) => role_id === targetRoleId )
    .map(({ permissions_id }) => permissions_id);
    
  return permissions
    .filter(({ permission_id }) => !permissionsByRole.includes(permission_id));
};

const targetRoleId = 2;
console.log(hasNotPermissionsByRole(permissionsAndRoles, permissions, targetRoleId));
.as-console-wrapper { max-height: 100% !important; top: 0 }

CodePudding user response:

As per my understanding, You want to get the filtered permissions which does not match with the permissions assigned to the roles in permissionsAndRoles array. If Yes, You can simply achieve it with the help of Array.filter() method along with Array.includes().

Live Demo :

const permissions = [{permission_id: 1, name: 'XXX'}, {permission_id: 2, name: 'XXX'}];

const roles = [{role_id: 1, role_name: 'XXX'}, {role_id: 2, role_name: 'XXX'}];

const permissionsAndRoles = [{id: 1, role_id: 1, permission_id:1}, {id:2, role_id: 2, permission_id: 2},{id:3, role_id: 1, permission_id: 2}];

function filteredArray(roleId) {
  const filteredPermissionAndRoles = permissionsAndRoles.filter(({ role_id }) => role_id === roleId).map(obj => obj.permission_id);
  const filteredPermissions = permissions.filter(({ permission_id }) => !filteredPermissionAndRoles.includes(permission_id));
  return filteredPermissions;
}

const res = filteredArray(2);

console.log(res);

  • Related