本文介绍了根据权限名称过滤用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列具有权限的用户.我想过滤出具有操作员权限的用户.
I have an array of users which have an array of permissions. I want to filter out users that have the operator permission.
用户列表
[
{
EmailAddress: "[email protected]",
FullName: "Test User",
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: "operator",
roleName: "operator",
roleid: 8,
}
],
UserID: 105,
},
{
EmailAddress: "[email protected]",
FullName: "tested User",
Permissions: [
{
PermissionData: null,
PermissionID: 205,
PermissionName: "admin",
roleName: "Admin",
roleid: 5,
}
],
UserID: 101,
},
]
我已经尝试过了,但是并没有获得所有具有操作员权限的用户
I have tried this however it doesn't get all users that have operator permissions
data.filter(x => {
for (const permission of x.Permissions) {
return permission['PermissionName'] === 'boc-operator';
}
});
推荐答案
您可以执行以下操作:
const data = [
{
EmailAddress: '[email protected]',
FullName: 'Test User',
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: 'operator',
roleName: 'operator',
roleid: 8,
},
],
UserID: 105,
},
{
EmailAddress: '[email protected]',
FullName: 'tested User',
Permissions: [
{
PermissionData: null,
PermissionID: 205,
PermissionName: 'admin',
roleName: 'Admin',
roleid: 5,
},
],
UserID: 101,
},
];
const hasPermission = (permissionName) => (item) =>
item.Permissions.some(
(permission) =>
permission.PermissionName === permissionName,
);
console.log(data.filter(hasPermission('admin')))
如果要在多个字段上进行过滤,则可以轻松扩展此范围:
If you want to filter on multiple fields you can easily extend this:
const data = [
{
EmailAddress: '[email protected]',
FullName: 'Test User',
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: 'operator',
roleName: 'operator',
roleid: 8,
},
],
UserID: 105,
},
{
EmailAddress: '[email protected]',
FullName: 'tested User',
Permissions: [
{
PermissionData: null,
PermissionID: 205,
PermissionName: 'admin',
roleName: 'Admin',
roleid: 5,
},
],
UserID: 101,
},
];
const hasPermission = (permissionName) => (item) =>
item.Permissions.some(
(permission) =>
permission.PermissionName === permissionName,
);
const emailContains = (value) => (item) =>
item.EmailAddress.includes(value);
const createFilter = (...filterFunctions) => (item) =>
filterFunctions.reduce(
(result, fn) => result && fn(item),
true,
);
console.log(
data.filter(
createFilter(
hasPermission('admin'),
emailContains('tested'),
),
)
);
最后;从对象获取属性的示例是它自己的函数.现在,您拥有的功能只能实现一件事,并且可以通过多种方式重复使用:
And finally; an example where getting the property from the object is in it's own function. Now you have functions that only implement one thing and can be re used in many ways:
const data = [
{
EmailAddress: '[email protected]',
FullName: 'Test User',
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: 'operator',
roleName: 'operator',
roleid: 8,
},
],
UserID: 105,
},
{
EmailAddress: '[email protected]',
FullName: 'tested User',
Permissions: [
{
PermissionData: null,
PermissionID: 205,
PermissionName: 'admin',
roleName: 'Admin',
roleid: 5,
},
],
UserID: 101,
},
];
const getPermissions = (item) => item.Permissions;
const getPermissionName = (item) => item.PermissionName;
const getEmailAddress = (item) => item.EmailAddress;
const isIn = (comparer, getter) => (value) => (item) =>
getter(item).some(comparer(value));
const isSame = (getter) => (value) => (item) =>
getter(item) === value;
const contains = (getter) => (value) => (item) =>
getter(item).includes(value);
const hasPermission = isIn(
isSame(getPermissionName),
getPermissions,
);
const emailContains = contains(getEmailAddress);
const createFilter = (...filterFunctions) => (item) =>
filterFunctions.reduce(
(result, fn) => result && fn(item),
true,
);
console.log(
data.filter(
createFilter(
hasPermission('admin'),
emailContains('tested'),
),
),
);
这篇关于根据权限名称过滤用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!