我有两个对象:
1。 menudata:
" [{" CATEGORY_ID":6," CATEGORY_NAME":"菜单1"" PARENT_ID":1,& #34;类型":"夹"" PARENT_NAME":"菜单1"" page_path":" - & #34;"状态":" 1528796405091"},{" CATEGORY_ID":7," CATEGORY_NAME":" A&# 34;," PARENT_ID":6,"类型":"夹"" PARENT_NAME":" A&#34 ;, " page_path":" - ""状态":" 1528796433246"},{" CATEGORY_ID":8, " CATEGORY_NAME":" B"" PARENT_ID":7,"类型":"夹"&#34 ; PARENT_NAME":" B"" page_path":" - ""状态":" 1528796433298&#34 ;},{" CATEGORY_ID":9," CATEGORY_NAME":" C"" PARENT_ID":8,"类型&# 34;:"夹"" PARENT_NAME":" C"" page_path":" - ",& #34;状态":" 1528796433340"},{" CATEGORY_ID":10," CATEGORY_NAME":" d"&# 34; PARENT_ID":9,"类型&# 34;:"夹"" PARENT_NAME":" d"" page_path":" - ",& #34;状态":" 1528796433394"},{" CATEGORY_ID" 451" CATEGORY_NAME":" E"&# 34; PARENT_ID":10,"类型":"文件"" PARENT_NAME":" E"" page_path& #34;:" ABC""状态":" 1528796433423"},{" CATEGORY_ID":103," CATEGORY_NAME&# 34;:"使用UserPreferences"" PARENT_ID":3,"类型":"文件"" PARENT_NAME&#34 ;: "&使用UserPreferences#34;" page_path":" ../../ JSP /配置/ UserPreferences.jsp""状态":空} ,{" CATEGORY_ID" 101" CATEGORY_NAME":" ADDUSER"" PARENT_ID&#34:4,"类型" :"文件"" PARENT_NAME":" ADDUSER"" page_path":" ../../ JSP /配置/newuser.jsp","state":null},{"category_id":4,"category_name":"UserAccess" ;, " PARENT_ID":2"类型":"夹",& #34; PARENT_NAME":" UserAccess"" page_path":" - ""状态":空},{& #34; CATEGORY_ID":3," CATEGORY_NAME":"设定"" PARENT_ID":2"类型":&# 34;夹"" PARENT_NAME":"设定"" page_path":" - ""状态&# 34;:空},{" CATEGORY_ID":2" CATEGORY_NAME":" SystemAdministration"" PARENT_ID":1,&#34 ;类型":"夹"" PARENT_NAME":" SystemAdministration"" page_path":" - &#34 ;,"状态":空}]"
2。 menurolesdata:
" [{" PAGE_ID" 101" CATEGORY_ID&#34:4},{" PAGE_ID":103," CATEGORY_ID&# 34;:3},{" PAGE_ID" 451" CATEGORY_ID" 10}]"
此处menudata对象包含所有父子(pageid和categoryid)记录,而menurolesdata包含具有某些特定访问权限的父和子。所以基本上我只想要那些存在于menurolesdata中的记录以及所有父母,直到它没有父母。
实施例。 menurlesdata中的page_id 451有父10,10有父9,9有父7,7有父6,6有父1和1没有父。
所以基本上我想根据menurolesdata过滤menudata。
注意:menurolesdata中的page_id与menudata中的category_id相同
以下是我的代码:
getMenu() {
Promise.all([this.dashboardServerice.getMenuBar(), this.dashboardServerice.getMenuRolesDetails()).then((result) => {
let menuData = result[0];
let menuRolesData = result[1];
menuData.forEach((item,index) => {
menuRolesData.forEach((data,key) => {
if(data.page_id == item.category_id && data.category_id == item.parent_id){
console.log("item===<>", item);
this.getAllParent(menuData, data.page_id);
}
});
});
});
}
// Following is my getAllParent() recursive function:
getAllParent(arr, pageid) {
let result = [];
for(let i in arr) {
if(arr[i].category_id == pageid) {
let parent = this.getAllParent(arr, arr[i].parent_id)
if(parent.length) {
arr[i].parent_id = parent
}
result.push(arr[i])
}
}
console.log("result ====================", result);
return result
}
我在这里只获得单一结果,即
{category_id: 101, category_name: "AddUser", parent_id: Array(1), type: "file", parent_name: "AddUser", …}
答案 0 :(得分:1)
虽然可以优化
,但我认为这是您想要的效果const menuData = [{ "category_id": 6, "category_name": "Menu1", "parent_id": 1, "type": "folder", "parent_name": "Menu1", "page_path": "-", "state": "1528796405091" }, { "category_id": 7, "category_name": "A", "parent_id": 6, "type": "folder", "parent_name": "A", "page_path": "-", "state": "1528796433246" }, { "category_id": 8, "category_name": "B", "parent_id": 7, "type": "folder", "parent_name": "B", "page_path": "-", "state": "1528796433298" }, { "category_id": 9, "category_name": "C", "parent_id": 8, "type": "folder", "parent_name": "C", "page_path": "-", "state": "1528796433340" }, { "category_id": 10, "category_name": "D", "parent_id": 9, "type": "folder", "parent_name": "D", "page_path": "-", "state": "1528796433394" }, { "category_id": 451, "category_name": "E", "parent_id": 10, "type": "file", "parent_name": "E", "page_path": "abc", "state": "1528796433423" }, { "category_id": 103, "category_name": "UserPreferences", "parent_id": 3, "type": "file", "parent_name": "UserPreferences", "page_path": "../../jsp/configuration/UserPreferences.jsp", "state": null }, { "category_id": 101, "category_name": "AddUser", "parent_id": 4, "type": "file", "parent_name": "AddUser", "page_path": "../../jsp/configuration/newuser.jsp", "state": null }, { "category_id": 4, "category_name": "UserAccess", "parent_id": 2, "type": "folder", "parent_name": "UserAccess", "page_path": "-", "state": null }, { "category_id": 3, "category_name": "Preferences", "parent_id": 2, "type": "folder", "parent_name": "Preferences", "page_path": "-", "state": null }, { "category_id": 2, "category_name": "SystemAdministration", "parent_id": 1, "type": "folder", "parent_name": "SystemAdministration", "page_path": "-", "state": null }];
const menuRolesData = [{ "page_id": 101, "category_id": 4 }, { "page_id": 103, "category_id": 3 }, { "page_id": 451, "category_id": 10 }];
function getMenuData(all, role) {
let menu = menuData.find(menu => menu.category_id === role.page_id);
while (menu) {
all.add(menu);
menu = menuData.find(parent => parent.category_id === menu.parent_id);
}
return all;
}
const result = menuRolesData.reduce((all, role) => getMenuData(all, role), new Set());
console.log(Array.from(result));
这是编辑link