// 提取 auth 对应的 id
extractMatchingAuths(arr1, arr2) {
const authSet = new Set(arr2);
const result = [];
const checkMenuAuthens = menuAuthens => {
if (!menuAuthens) return;
for (const menu of menuAuthens) {
if (authSet.has(menu.auth)) {
console.log(menu.auth);
result.push(menu.id);
}
checkMenuAuthens(menu.menuAuthens);
}
};
for (const item of arr1) {
if (authSet.has(item.auth)) {
result.push(item.id);
}
checkMenuAuthens(item.menuAuthens);
}
return result;
},
数据示例:
arr1=[
{
"id":"A1",
"dadId":null,
"menuName":"仓库管理",
"auth":"auth-a1",
"url":null,
"sort":1,
"icon":"cangku",
"menuAuthens":[
{
"id":"A2",
"dadId":"A1",
"menuName":"物资入库",
"auth":"auth-a2",
"url":"/keepView",
"sort":1,
"icon":null,
"menuAuthens":[
{
"id":"A11",
"dadId":"A2",
"menuName":"详情",
"auth":"goodsKeep:info",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A12",
"dadId":"A2",
"menuName":"添加入库",
"auth":"goodsKeep:upd",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A13",
"dadId":"A2",
"menuName":"删除",
"auth":"goodsKeep:del",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A14",
"dadId":"A2",
"menuName":"入库",
"auth":"goodsKeep:join",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A29",
"dadId":"A2",
"menuName":"导出",
"auth":"goodsKeep:excel",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
}
]
},
{
"id":"A3",
"dadId":"A1",
"menuName":"物资出库",
"auth":"auth-a3",
"url":"/outflowView",
"sort":2,
"icon":null,
"menuAuthens":[
{
"id":"A16",
"dadId":"A3",
"menuName":"详情",
"auth":"goodsOut:info",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A17",
"dadId":"A3",
"menuName":"添加出库",
"auth":"goodsOut:upd",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A18",
"dadId":"A3",
"menuName":"删除",
"auth":"goodsOut:del",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A19",
"dadId":"A3",
"menuName":"出库",
"auth":"goodsOut:out",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
}
]
},
{
"id":"A5",
"dadId":"A1",
"menuName":"OCR识别",
"auth":"auth-a5",
"url":"/OCRView",
"sort":4,
"icon":null,
"menuAuthens":{
}
},
{
"id":"A6",
"dadId":"A1",
"menuName":"出库警告",
"auth":"auth-a6",
"url":"/outWarringView",
"sort":5,
"icon":null,
"menuAuthens":{
}
},
{
"id":"A7",
"dadId":"A1",
"menuName":"库存预警",
"auth":"auth-a7",
"url":"/stockView",
"sort":6,
"icon":null,
"menuAuthens":[
{
"id":"A21",
"dadId":"A7",
"menuName":"详情",
"auth":"keepWarring:info",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A22",
"dadId":"A7",
"menuName":"添加预警",
"auth":"keepWarring:upd",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A23",
"dadId":"A7",
"menuName":"删除",
"auth":"keepWarring:del",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
}
]
},
{
"id":"A8",
"dadId":"A1",
"menuName":"报废处理",
"auth":"auth-a8",
"url":"/abandonView",
"sort":7,
"icon":null,
"menuAuthens":[
{
"id":"A24",
"dadId":"A8",
"menuName":"报废列表",
"auth":"goodsScrap:list",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A25",
"dadId":"A8",
"menuName":"详情",
"auth":"goodsScrap:info",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A26",
"dadId":"A8",
"menuName":"添加报废",
"auth":"goodsScrap:upd",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A27",
"dadId":"A8",
"menuName":"删除",
"auth":"goodsScrap:del",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
},
{
"id":"A28",
"dadId":"A8",
"menuName":"候补单据",
"auth":"goodsScrap:form",
"url":"",
"sort":999,
"icon":null,
"menuAuthens":null
}
]
},
{
"id":"A9",
"dadId":"A1",
"menuName":"物资盘点",
"auth":"auth-a9",
"url":"/InventoryOfMaterials",
"sort":8,
"icon":null,
"menuAuthens":{
}
}
]
}
]
arr2 = [
"auth-a1",
"auth-a2",
"auth-a3",
"auth-a5",
"auth-a6",
"auth-a7",
"auth-a8",
"auth-b1",
"auth-b2",
"auth-b3",
"auth-b4",
"auth-b5",
"auth-a9",
"auth-b6",
"auth-c1",
"auth-c2",
"auth-c3",
"auth-d1",
"auth-d2",
"auth-e1",
"auth-e2",
"auth-f1",
"auth-f2",
"auth-f3",
"auth-g1",
"auth-g2",
"auth-f4",
"admin:all",
"goodsKeep:list",
"goodsKeep:info",
"goodsKeep:upd",
"goodsKeep:del",
"goodsKeep:join",
"goodsOut:list",
"goodsOut:info",
"goodsOut:upd",
"goodsOut:del",
"goodsOut:out",
"keepWarring:list",
"keepWarring:info",
"keepWarring:upd",
"keepWarring:del",
"goodsScrap:list",
"goodsScrap:info",
"goodsScrap:upd",
"goodsScrap:del",
"goodsScrap:form",
"goodsKeep:excel"
]