Why do you want to add one more for filtering? just add a flag to the first object list.
const router = [
{
path: '/hello',
icon: 'hammer',
name: 'hello',
title: '',
children: [
{ path: 'index', title: '', name: 'hello_index' },
{ path: 'index1', title: '1', name: 'hello_index1' }
]
},
{
path: '/hello1',
icon: 'hammer',
name: 'hello1',
title: '',
children: [
{ path: 'index2', title: '2', name: 'hello_index2',meta: {noshow:true} },
{ path: 'index3', title: '3', name: 'hello_index3' ,meta: {noshow:true}},
{ path: 'index4', title: '3', name: 'hello_index4' }
]
},
{
path: '/hello2',
icon: 'hammer',
name: 'hello2',
title: '',
meta: {noshow:true},
children: [
{ path: 'index4', title: '4', name: 'hello_index4' },
{ path: 'index5', title: '5', name: 'hello_index5' }
]
}
]
it would be nice to judge that the noshow in meta is true, at run time.
violence screening, all we can do is to optimize the degree of violence;
let router = [
{
path: '/hello',
icon: 'hammer',
name: 'hello',
title: '',
children: [
{ path: 'index', title: '', name: 'hello_index' },
{ path: 'index1', title: '1', name: 'hello_index1' }
]
},
{
path: '/hello1',
icon: 'hammer',
name: 'hello1',
title: '',
children: [
{ path: 'index2', title: '2', name: 'hello_index2' },
{ path: 'index3', title: '3', name: 'hello_index3' },
{ path: 'index4', title: '3', name: 'hello_index4' }
]
},
{
path: '/hello2',
icon: 'hammer',
name: 'hello2',
title: '',
children: [
{ path: 'index4', title: '4', name: 'hello_index4' },
{ path: 'index5', title: '5', name: 'hello_index5' }
]
}
]
let routernone = {
'/hello2': [],
'/hello1': ['index2', 'index3']
}
router = router.filter((route, index) => {
if (routernone[route.path]){
if (routernone[route.path].length > 0) {
route.children = route.children.filter((child)=> {
return routernone[route.path].indexOf(child.path) <= -1
})
return true
} else {
return false
}
} else {
return true
}
})
console.log(router)
let router = [
{
path: '/hello',
icon: 'hammer',
name: 'hello',
title: '',
children: [
{ path: 'index', title: '', name: 'hello_index' },
{ path: 'index1', title: '1', name: 'hello_index1' }
]
},
{
path: '/hello1',
icon: 'hammer',
name: 'hello1',
title: '',
children: [
{ path: 'index2', title: '2', name: 'hello_index2' },
{ path: 'index3', title: '3', name: 'hello_index3' },
{ path: 'index4', title: '3', name: 'hello_index4' }
]
},
{
path: '/hello2',
icon: 'hammer',
name: 'hello2',
title: '',
children: [
{ path: 'index4', title: '4', name: 'hello_index4' },
{ path: 'index5', title: '5', name: 'hello_index5' }
]
}
];
let routernone = [
{ path: '/hello2'}
];
let routernone2 = [
{
path: '/hello1',
children: [
'index2', 'index3'
]
}
];
router = router.filter(item => {
return routernone.findIndex( i => item.path == i.path) == -1;
}).map(item => {
let temp = routernone2.find(i => i.path == item.path);
if(temp){
item.children = item.children.filter(ii => !temp.children.find(iii => iii == ii.path));
}
return item;
});