简体   繁体   English

如何根据其他 arrays 的值过滤数组?

[英]How can I filter an array based on values ​from other arrays?

I have a problem with filtering the data array.我在过滤数据数组时遇到问题。

I have three arrays:我有三个 arrays:

permissions:[] - The array has a list of permissions, that is: permission_id,name. permissions:[] - 该数组有一个权限列表,即: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.我在这里,因为我不知道如何很好地将这种过滤的整个逻辑组合在一起。

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 }

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.据我了解,您希望获得与分配给permissionsAndRoles数组中角色的权限不匹配的过滤权限。 If Yes, You can simply achieve it with the help of Array.filter() method along with Array.includes() .如果是,您可以简单地借助Array.filter()方法和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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于数组的值过滤数组的数组 - How to filter an array of arrays based on values of an array 如何基于其他 arrays 过滤具有嵌套 arrays 的对象数组 - How to filter array of objects with nested arrays based on other arrays 如何根据其他 arrays 对象的嵌套 arrays 过滤对象数组 - How to filter array of objects with nested arrays of objects based on other arrays 如何根据其嵌套数组之一的值过滤数组? - How do I filter an array based on the values of one of its nested arrays? 基于2个值将数组数组过滤为唯一? - Filter Array of Arrays to Unique based off 2 values? 当第一个数组设置为显示随机值时,如何关联两个数组的从零开始的值 - How can I associate zero-based values from two arrays when the first array is set to display a random value 如何使用.filter() 从数组中删除值? - How can i delete values from an array using .filter()? 如何将 arrays 数组转换为对象数组,从其他数组中选择键? - How can I convert an array of arrays to an array of objects, selecting the key from other array? 如何在其他对象数组的每个值上创建多个数组? - How can I create several arrays on each of the values of other array of objects? 如何根据 javascript 中的其他数组过滤子数组的角色 - how to filter role from children of array based on other array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM