我有一系列权限
const permissions = ['search', 'reports', 'whitelist'];
我有一个菜单
const menus = [
{
text: 'Navigation',
link: 'navigation',
topic: 'all',
},
{
text: 'Dictionary',
link: 'dict',
topic: 'src',
},
{
text: 'Services',
link: '/services',
submenu: [
{
text: 'Check company',
link: '/services/search',
topic: 'search',
},
{
text: 'Report statistic',
link: '/services/reportstatistic',
topic: 'another-topic',
},
{
text: 'Report request',
link: '/services/getrequestreport',
topic: 'another-topic-2',
},
{
text: 'Get whitelist',
link: '/services/getresponsereport',
topic: 'whitelist',
},
{
text: 'Get data',
link: '/services/requestdata',
topic: 'reports',
},
],
},
];
我需要过滤menus以检查数组的每一项是否topic等于数组中的一项权限permissions,如果它具有submenu,则检查每个项目表单子菜单是否相同或是否topic ==='all'并将其添加到新菜单中。过滤后的预期结果是
const newArrr = [
{
text: 'Navigation',
link: 'navigation',
topic: 'all',
},
{
text: 'Services',
link: '/services',
submenu: [
{
text: 'Check company',
link: '/services/search',
topic: 'search',
},
{
text: 'Get whitelist',
link: '/services/getresponsereport',
topic: 'whitelist',
},
{
text: 'Get data',
link: '/services/requestdata',
topic: 'reports',
},
],
},
];
我试图制作自己的循环,但它不正确
for (let menu of menus) {
for (let permission of filteredPermissions) {
if (menu.topic && (menu.topic == permission || menu.topic === 'all')) {
newArr.push(menu);
}
if (menu.submenu) {
for (let submenu of menu.submenu) {
for (let permissionSecond of filteredPermissions) {
if (!(submenu.topic == permissionSecond) || !(submenu == 'all')) {
menu.submenu.splice(menu.submenu[submenu], 1);
}
}
}
}
}
}
四季花海
波斯汪
相关分类