项目中有个要求根据数据的role字段是否包含在数组中进行筛选数据
const permissions = ['admin','system']
const treeData = [
{
key:"1",
name:"aaa",
role:"admin",
children:[
{
key:"1-1",
name:"1-1",
role:"system",
},
{
key:"1-2",
name:"1-2",
role:"system-admin",
}
]
},
{
key:"2",
name:"bbb",
role:"admin",
children:[
{
key:"2-1",
name:"2-1",
role:"system",
},
{
key:"2-2",
name:"2-2",
role:"system-admin",
}
]
}
]
getTree = tree => {
const newTree = tree.filter(item => permissions.indexOf(item.role) !== -1)
return newTree.map(item => {
if (item.children) {
item.children = getTree (item.children)
}
return item
})
}
getTree(treeData) // 这个就是过滤后的数据