I have an array of users which have an array of permissions. I want to filter out users that have the operator permission.
User List
[
{
EmailAddress: "test@test.com",
FullName: "Test User",
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: "operator",
roleName: "operator",
roleid: 8,
}
],
UserID: 105,
},
{
EmailAddress: "tested@test.com",
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';
}
});
答案 0 :(得分:0)
You can user filter
and some api's something like
var data=[
{
EmailAddress: "test@test.com",
FullName: "Test User",
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: "operator",
roleName: "operator",
roleid: 8,
}
],
UserID: 105,
},
{
EmailAddress: "tested@test.com",
FullName: "tested User",
Permissions: [
{
PermissionData: null,
PermissionID: 205,
PermissionName: "admin",
roleName: "Admin",
roleid: 5,
}
],
UserID: 101,
},
]
var opratorUsers=data.filter(u=> u.Permissions && u.Permissions.some(p=> p.roleName=='operator'));
console.log(opratorUsers);
答案 1 :(得分:0)
You can do the following:
const data = [
{
EmailAddress: 'test@test.com',
FullName: 'Test User',
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: 'operator',
roleName: 'operator',
roleid: 8,
},
],
UserID: 105,
},
{
EmailAddress: 'tested@test.com',
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: 'test@test.com',
FullName: 'Test User',
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: 'operator',
roleName: 'operator',
roleid: 8,
},
],
UserID: 105,
},
{
EmailAddress: 'tested@test.com',
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: 'test@test.com',
FullName: 'Test User',
Permissions: [
{
PermissionData: null,
PermissionID: 202,
PermissionName: 'operator',
roleName: 'operator',
roleid: 8,
},
],
UserID: 105,
},
{
EmailAddress: 'tested@test.com',
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'),
),
),
);