You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于条件过滤数组?角色权限场景下的数组过滤咨询

Hey there! Let's walk through how to filter your role-based permissions array—first, I noticed your Roles has a small syntax quirk (it's a 2D array but was wrapped in curly braces instead of square brackets), so here's the corrected version we'll use for examples:

let Roles = [
  [ 
    { model: 'user', property: 'find', permission: 'allow' }, 
    { model: 'user', property: 'create', permission: 'allow' }, 
    { model: 'user', property: 'update', permission: 'deny' }, 
    { model: 'user', property: 'delete', permission: 'deny' } 
  ],
  [ 
    { model: 'registration', property: 'find', permission: 'allow' }, 
    { model: 'registration', property: 'create', permission: 'deny' }, 
    { model: 'registration', property: 'update', permission: 'allow' } // Filled in the missing permission for clarity
  ]
];

Now, let's cover common filtering scenarios using JavaScript's filter() method (and map()/flat() to handle the 2D structure):

1. Filter by a specific model (e.g., only 'user' permissions)

Keep the original 2D role structure

If you want to retain the separate role arrays but only keep permissions for a specific model:

const userOnlyPermissions = Roles.map(rolePermissions => 
  rolePermissions.filter(perm => perm.model === 'user')
);
// Result: First array stays intact, second array is empty (no 'user' permissions there)

Flatten to a single array of matching permissions

If you don't need to preserve the role groups and just want all matching permissions in one list:

const allUserPermissions = Roles.flat().filter(perm => perm.model === 'user');

2. Filter by permission type (e.g., only 'allow' permissions)

Keep 2D structure

const allowOnlyPermissions = Roles.map(rolePermissions => 
  rolePermissions.filter(perm => perm.permission === 'allow')
);

Flattened result

const allAllowPermissions = Roles.flat().filter(perm => perm.permission === 'allow');

3. Combine multiple conditions (e.g., 'user' model + 'allow' permission)

For more targeted filtering, chain conditions in the filter() callback:

const userAllowPermissions = Roles.flat().filter(perm => 
  perm.model === 'user' && perm.permission === 'allow'
);
// Result: [{model: 'user', property: 'find', permission: 'allow'}, {model: 'user', property: 'create', permission: 'allow'}]

4. Create a reusable filter function

If you need to filter with dynamic conditions (like different models/permissions on the fly), wrap the logic in a function:

function filterPermissions(rolesArray, filterCriteria) {
  // Flatten the 2D array and filter by all matching criteria
  return rolesArray.flat().filter(perm => {
    return Object.keys(filterCriteria).every(key => {
      return perm[key] === filterCriteria[key];
    });
  });
}

// Example usage: Get 'deny' permissions for the 'registration' model
const registrationDenyPermissions = filterPermissions(Roles, { 
  model: 'registration', 
  permission: 'deny' 
});

You can adjust these examples to fit any custom condition—just modify the callback in filter() to check the properties you care about!

内容的提问来源于stack exchange,提问作者Shaik Nizamuddin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:27:14