Vue深度优先遍历多层数组对象(相当于多棵树、三级树)
这个方法如果是对于下面的三级树的话可以拿到爷爷Id,自己Id,父亲Id;其实如果想要拿到label的话就把data.id换成data.label就行了function treeFindPath(tree, func, path = []) {if (!tree) return []for (const data of tree) {path.push(data.id)if (func(data))
·
这个方法如果是对于下面的三级树的话可以拿到爷爷Id,自己Id,父亲Id;其实如果想要拿到label的话就把data.id换成data.label就行了
function treeFindPath(tree, func, path = []) {
if (!tree) return []
for (const data of tree) {
path.push(data.id)
if (func(data)) return path
if (data.children) {
const findChildren = treeFindPath(data.children, func, path)
if (findChildren.length) return findChildren
}
path.pop()
}
return []
}
const i = treeFindPath(this.treeData, node => node.label === result)
比如树结构是这样的:这相当于是多可三级树
"data": [
{
"id": "1",
"label": "能动中心",
"type": "1",
"children": [
{
"id": "11",
"label": "罐底视频",
"type": "2",
"children": [
{
"id": "111",
"type": "3",
"label": "四高炉6道"
},
{
"id": "112",
"type": "3",
"label": "西渣罐底"
}
]
},
{
"id": "12",
"label": "煤气柜站",
"type": "2",
"children": [
{
"id": "121",
"type": "3",
"label": "13号道口02"
},
{
"id": "122",
"type": "3",
"label": "13号道口01"
},
{
"id": "123",
"type": "3",
"label": "能动中心楼顶"
}
]
},
{
"id": "13",
"label": "能动中心楼顶",
"type": "2",
"children": [
{
"id": "131",
"type": "3",
"label": "44455666"
}
]
}
]
},
{
"id": "2",
"label": "炼铁厂",
"type": "1",
"children": [
{
"id": "21",
"label": "星云智联",
"type": "2",
"children": [
{
"id": "211",
"type": "3",
"label": "程控楼3楼"
},
{
"id": "212",
"type": "3",
"label": "程控楼1楼过道西侧"
},
{
"id": "213",
"type": "3",
"label": "程控楼2楼大厅"
},
{
"id": "214",
"type": "3",
"label": "公司主楼5楼西侧"
}
]
},
{
"id": "22",
"label": "翻车机溜车线区域",
"type": "2",
"children": [
{
"id": "221",
"type": "3",
"label": "炼钢球罐全貌1"
}
]
},
{
"id": "23",
"label": "焦化化产作业区",
"type": "2",
"children": [
{
"id": "231",
"type": "3",
"label": "4#万立储槽全貌"
},
{
"id": "232",
"type": "3",
"label": "4#万立中压氧压机"
},
{
"id": "233",
"type": "3",
"label": "4#万立变电所低压室"
}
]
}
]
},
{
"id": "3",
"label": "炼钢厂",
"type": "1",
"children": [
{
"id": "31",
"label": "熔融金属及吊运区域",
"type": "2",
"children": [
{
"id": "311",
"type": "3",
"label": "8号吊点鞍马座"
},
{
"id": "312",
"type": "3",
"label": "8号起吊点右"
}
]
},
{
"id": "32",
"label": "区域监控",
"type": "2",
"children": [
{
"id": "321",
"type": "3",
"label": "测试点33"
},
{
"id": "322",
"type": "3",
"label": "原料跨1"
},
{
"id": "323",
"type": "3",
"label": "板坯LH钒铁柜"
}
]
},
{
"id": "33",
"label": "罐号识别",
"type": "2",
"children": [
{
"id": "331",
"type": "3",
"label": "修罐间东头"
}
]
}
]
},
{
"id": "4",
"label": "冷轧厂",
"type": "1",
"children": [
{
"id": "41",
"label": "轧钢作业区",
"type": "2",
"children": [
{
"id": "411",
"type": "3",
"label": "轧机主控室"
}
]
},
{
"id": "42",
"label": "普冷作业区",
"type": "2",
"children": [
{
"id": "421",
"type": "3",
"label": "原料库1"
},
{
"id": "422",
"type": "3",
"label": "原料库2"
}
]
},
{
"id": "43",
"label": "镀锌作业区",
"type": "2",
"children": [
{
"id": "431",
"type": "3",
"label": "生产运行检测"
},
{
"id": "432",
"type": "3",
"label": "轧机高压室"
}
]
},
{
"id": "44",
"label": "点检维护作业区",
"type": "2",
"children": [
{
"id": "441",
"type": "3",
"label": "退火炉4"
},
{
"id": "442",
"type": "3",
"label": "退火炉1"
}
]
}
]
}
]
更多推荐
已为社区贡献3条内容
所有评论(0)