js中递归树形数据,及查询该节点的所有父级节点、及有筛选符合条件下的递归数据
一、递归树形数据
getJsonTree(data) {
let showDate = data.map(item => ({
...item,
icon: 'el-icon-caret-bottom',
children:
item.childTags && item.childTags.length
? this.getJsonTree(item.childTags)
: null,
}))
return showDate
},
二、查询该节点的所有父节点
getCheckTag(list, id) {
for (let i in list) {
if (list[i].id === id) {
return [list[i]]
}
if (list[i].childTags != null) {
let node = this.getCheckTag(list[i].childTags, id)
if (node !== undefined) {
return node.concat(list[i])
}
}
}
},
链接: https://blog.csdn.net/Anything2207294253/article/details/123251549
三、筛选符合条件下的递归数据
最终数据 = data.map(item => this.mapTree(item))
mapTree(data) {
let haveChildren = Array.isArray(data.childTags) && data.childTags.length > 0
let children = haveChildren ? data.childTags.filter(item => item.type == 1) : null
return {
value: data.id,
label: data.name,
children: children ? children.map(i => this.mapTree(i)) : null,
}
},