为了账号安全,请及时绑定邮箱和手机立即绑定

如何过滤数组以匹配数组

如何过滤数组以匹配数组

噜噜哒 2021-06-02 16:08:02
我有权限列表: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 回答

?
互换的青春

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


查看完整回答
反对 回复 2021-06-11
?
墨色风雨

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)


查看完整回答
反对 回复 2021-06-11
  • 3 回答
  • 0 关注
  • 124 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信