提交 4d5bd09e authored 作者: 王鹏飞's avatar 王鹏飞

算法优化

上级 dda9f13d
......@@ -44,8 +44,7 @@ export default {
{ name: '学校管理系统', system_tag: 1, permissions: [], value: [] },
{ name: '教师端', system_tag: 2, permissions: [], value: [] },
{ name: '学生端', system_tag: 3, permissions: [], value: [] }
],
idsPath: []
]
}
},
async beforeMount() {
......@@ -108,9 +107,7 @@ export default {
let ids = []
item.value.forEach(id => {
// 通过子节点ID,返回包含父节点ID的数组
this.getIdPath(this.permissions, id)
ids = ids.concat(this.idsPath)
this.idsPath = []
ids = ids.concat(this.treeFindPath(this.permissions, id))
})
result.push({ system_tag: item.system_tag, permission_ids: this.uniqueIds(ids) })
}
......@@ -129,19 +126,20 @@ export default {
})
},
// 通过子节点ID,返回路径数组
getIdPath(tree, targetId) {
for (let index = 0; index < tree.length; index++) {
if (tree[index].children) {
if (this.getIdPath(tree[index].children, targetId)) {
this.idsPath.push(tree[index].id)
return true
}
}
if (tree[index].id === targetId) {
this.idsPath.push(tree[index].id)
return true
// https://segmentfault.com/q/1010000020095550
treeFindPath(tree, targetId, path = []) {
if (!tree) return []
for (const data of tree) {
// 这里按照你的需求来存放最后返回的内容吧
path.push(data.id)
if (data.id === targetId) return path
if (data.children) {
const findChildren = this.treeFindPath(data.children, targetId, path)
if (findChildren.length) return findChildren
}
path.pop()
}
return []
},
// ID去重
uniqueIds(ids) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论