如何过滤数组以匹配数组

时间:2019-05-27 05:44:49

标签: javascript arrays ecmascript-6

我有权限列表:

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? 谢谢

3 个答案:

答案 0 :(得分:2)

您可以使用filterincludes

  • 使用过滤器遍历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)

使用filterincludes就像这样:

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; }