我有权限列表:
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]
如何过滤all_permissions以仅匹配my_permissions中的ID? 谢谢
答案 0 :(得分:2)
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)
答案 1 :(得分:1)
您可以尝试使用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);
答案 2 :(得分:0)
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 res = all_permissions.filter(({ permission_id }) => my_permissions.includes(permission_id));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
ES5语法:
var 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]+$/}];
var my_permissions = [1,2];
var res = all_permissions.filter(function(item) {
return my_permissions.indexOf(item.permission_id) > -1;
});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }