我想在比较两个数组后 return true or false
I want to return true or false after comparing two arrays
我想在比较两个数组后return真或假。
这里是第一个数组:
const permissionsArray = [
{
"groupName": "User",
"permissions": [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
"Permissions.Users.Delete"
]
}, {
"groupName": "Role",
"permissions": [
"Permissions.Roles.View",
"Permissions.Roles.Create",
"Permissions.Roles.Edit",
"Permissions.Roles.Delete"
]
},
]
此处包含权限数组的对象:
const userPermissionArray =
{
"id": "cdfda4d3-ea1a-4662-a611-2d2e52b772b0",
"name": "ABP Manager",
"description": "ABP Manager",
"usersCount": 4,
"isActive": false,
"permissions": [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
"Permissions.Users.Delete"
]
}
我想比较 [permissionsArray.permissions] 和 [userPermissionArray.permissions].
我试过了,但我失败了。我的代码在这里:
console.log(
userPermissionArray.permissions
.map(roleItem => roleItem === userPermissionArray
.map(item => item.permissions.map(item => item))
)
);
我正在尝试 return 'true' 或 'false'。
您可以flat
权限列表以便于比较,然后使用some
功能检查是否存在任何条目
let flatPermissions = permissionsArray.map(e => e["permissions"]).flat();
let result = userPermissionArray.permissions.some(i => flatPermissions.includes(i));
console.log(result);
试着这样比较:
let permissionsA = [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
"Permissions.Users.Delete"
]
let permissionsB = [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
]
let rs = permissionsA.reduce((a, b) => {
return {
...a,
[b]: permissionsB.includes(b)
}
}, {})
console.log(rs)
/*
{
Permissions.Users.Create: true,
Permissions.Users.Delete: false,
Permissions.Users.Edit: true,
Permissions.Users.View: true
} */
我想在比较两个数组后return真或假。
这里是第一个数组:
const permissionsArray = [
{
"groupName": "User",
"permissions": [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
"Permissions.Users.Delete"
]
}, {
"groupName": "Role",
"permissions": [
"Permissions.Roles.View",
"Permissions.Roles.Create",
"Permissions.Roles.Edit",
"Permissions.Roles.Delete"
]
},
]
此处包含权限数组的对象:
const userPermissionArray =
{
"id": "cdfda4d3-ea1a-4662-a611-2d2e52b772b0",
"name": "ABP Manager",
"description": "ABP Manager",
"usersCount": 4,
"isActive": false,
"permissions": [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
"Permissions.Users.Delete"
]
}
我想比较 [permissionsArray.permissions] 和 [userPermissionArray.permissions].
我试过了,但我失败了。我的代码在这里:
console.log(
userPermissionArray.permissions
.map(roleItem => roleItem === userPermissionArray
.map(item => item.permissions.map(item => item))
)
);
我正在尝试 return 'true' 或 'false'。
您可以flat
权限列表以便于比较,然后使用some
功能检查是否存在任何条目
let flatPermissions = permissionsArray.map(e => e["permissions"]).flat();
let result = userPermissionArray.permissions.some(i => flatPermissions.includes(i));
console.log(result);
试着这样比较:
let permissionsA = [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
"Permissions.Users.Delete"
]
let permissionsB = [
"Permissions.Users.View",
"Permissions.Users.Create",
"Permissions.Users.Edit",
]
let rs = permissionsA.reduce((a, b) => {
return {
...a,
[b]: permissionsB.includes(b)
}
}, {})
console.log(rs)
/*
{
Permissions.Users.Create: true,
Permissions.Users.Delete: false,
Permissions.Users.Edit: true,
Permissions.Users.View: true
} */