亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數組中如何按照規則篩選對象?

數組中如何按照規則篩選對象?

楊__羊羊 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 關注
  • 488 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號