如何基于条件过滤数组?角色权限场景下的数组过滤咨询
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

