3 回答
TA贡献1797条经验 获得超6个赞
你可以试试Array.prototype.filter()和Array.prototype.includes()
const all_permissions = [
{
permission_id: 1,
permission_name: "Getting User List",
permission_method: "GET",
permission_url: /^\/panel\/user$/
},
{
permission_id: 2,
permission_name: "Getting User Info",
permission_method: "GET",
permission_url: /^\/panel\/user\/[0-9]+$/
},
{
permission_id: 3,
permission_name: "Testing",
permission_method: "GET",
permission_url: /^\/panel\/test\/[a-z]+$/
}
]
const my_permissions = [1,2];
const match = all_permissions.filter(p => my_permissions.includes(p.permission_id));
console.log(match);
TA贡献1853条经验 获得超6个赞
您可以使用过滤器并包含
all_permission使用过滤器循环遍历数组
内部过滤器检查permission_id在my_permission数组,如果它的存在,返回true否则返回false
const all_permissions = [{permission_id: 1,permission_name: "Getting User List",permission_method: "GET",permission_url: /^\/panel\/user$/},{permission_id: 2,permission_name: "Getting User Info",permission_method: "GET",permission_url: /^\/panel\/user\/[0-9]+$/},{permission_id: 3,permission_name: "Testing",permission_method: "GET",permission_url: /^\/panel\/test\/[a-z]+$/}]
const my_permissions = [1,2]
let op = all_permissions.filter(({permission_id})=> my_permissions.includes(permission_id))
console.log(op)
添加回答
举报