首先要找到可以判断的条件
得要把一个数组转成对象这样就不用写那么多的循环优化代码
例如
per: [
{ id: 3, user_id: 2, path: "/category", path_name: "分类页面" },
{ id: 4, user_id: 2, path: "/good", path_name: "商品页面" },
{ id: 6, user_id: 2, path: "/order", path_name: "订单页面" },
],
menuArr: [
{
icon: "el-icon-location",
title: "商品管理",
subs: [
{ title: "商品列表", index: "/good" },
{ title: "商品分类", index: "/category" },
{ title: "品牌管理", index: "/brand" },
{ title: "商品属性管理", index: "/attribute" },
],
},
{
icon: "el-icon-location",
title: "订单中心",
subs: [{ title: "订单列表", index: "/order" }],
},
{
icon: "el-icon-location",
title: "店铺运营",
subs: [
{ title: "专题管理", index: "/topic" },
{ title: "运营数据", index: "/statdata" },
],
},
],
let newMenu = [];
let per = this.$store.state.per;
let perObj = per.reduce((pre, current) => {
pre[current.path] = current;
return pre;
}, {});
let menuArr = this.menuArr;
for (var i = 0; i < menuArr.length; i++) {
let arr = [];
for (var j = 0; j < menuArr[i].subs.length; j++) {
let subsItem = menuArr[i].subs[j];
let path = subsItem.index;
if (perObj[path]) {
arr.push(subsItem);
}
}
if (arr.length !== 0) {
newMenu.push({
...menuArr[i],
subs: arr,
});
}
}
this.menuArr=newMenu;
},