使用splice去除数组中指定的的项,结果不对
如下的代码,我需要保留children.path===allOrder || children.path===accessPage的项
let routers = [
{
path: '/index',
name: 'Index',
children: [{
path: 'accountManagement',
name: 'accountManagement',
meta: { title: '权限管理', icon: 'el-icon-setting' }
}]
},
{
path: '/index',
name: 'paltform logs',
meta: { title: '平台日志', icon: 'el-icon-menu' },
children: [{
path: 'UserBehavior',
meta: { title: '用户行为' },
name: 'UserBehavior'
}, {
path: 'errorOrder',
meta: { title: '错误订单' },
name: 'errorOrder'
}, {
path: 'statisticRecharge',
meta: { title: '统计充值' },
name: 'StatisticRecharge'
},
{
path: 'allOrder',
meta: { title: '所有订单' },
name: 'allOrder'
}, {
path: 'userRechargeError',
meta: { title: '用户充值总金额错误' },
name: 'userRechargeError'
}
]
},
{
path: '/index',
name: 'wechat api',
meta: { title: '微信接口', icon: 'el-icon-menu' },
children: [{
path: 'chart',
meta: { title: '访问趋势' },
name: 'Chart'
}, {
path: 'visitDistribution',
meta: { title: '访问分布' },
name: 'VisitDistribution'
}, {
path: 'accessRetention',
meta: { title: '访问留存' },
name: 'AccessRetention'
}, {
path: 'accessPage',
meta: { title: '访问页面' },
name: 'accessPage'
}, {
path: 'userPortrait',
meta: { title: '用户画像' },
name: 'UserPortrait'
}, {
path: 'payStatistics',
meta: { title: '付费统计' },
name: 'PayStatistics'
}]
}
]
function filterAsyncRouter(Router) {
console.log(Router)
if(Router && Router.length){
for(let i=0;i<Router.length;i++){
if(Router[i].name && Router[i].children){
for(let k=0;k<Router[i].children.length;k++){
if(!hasPermissionName(Router[i].children[k].name)){
Router[i].children.splice(k,1)
k++
}
}
}
}
}
console.log(Router)
}
function hasPermissionName(routerName) {
let permissions = ['allOrder','accessPage']
for (let i = 0; i < permissions.length; i++) {
if(permissions[i]===routerName){
return true
}
}
}
filterAsyncRouter(routers)
k++改成k--