为了账号安全,请及时绑定邮箱和手机立即绑定

数组中如何按照规则筛选对象?

数组中如何按照规则筛选对象?

杨__羊羊 2019-05-21 15:52:03
constrouter=[{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'}]}]letrouternone=[{path:'/hello2',children:[]},{path:'/hello1',children:['index2','index3']}];请问如何在router以routernone为规则筛选出新数组,routernone中表明,不取path为'/hello2'的,不取path为'/hello1'的children中path为index2的和path为index3的,
查看完整描述

2 回答

?
莫回无

TA贡献1865条经验 获得超7个赞

暴力筛选,能做的只是优化暴力程度;
letrouter=[
{
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'}
]
}
]
letrouternone={
'/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)=>{
returnrouternone[route.path].indexOf(child.path)<=-1
})
returntrue
}else{
returnfalse
}
}else{
returntrue
}
})
console.log(router)
                            
查看完整回答
反对 回复 2019-05-21
?
绝地无双

TA贡献1946条经验 获得超4个赞

为什么要多加一个用于筛选的呢,直接在第一个对象列表中加标志不就好了,
constrouter=[
{
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'}
]
}
]
在运行时判断一下meta里的noshow是true,就不显示不就好了
                            
查看完整回答
反对 回复 2019-05-21
  • 2 回答
  • 0 关注
  • 494 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号