文件结构如下所示
控制台打印输出
代码:
getPathById(value, arr) {
const tempPath = []
try {
for (let i = 0; i < arr.length; i++) {
this.getNodePath(arr[i], tempPath, value)
}
} catch (e) {
return tempPath
}
},
getNodePath(node, tempPath, value) {
tempPath.push(node.id)
if (node.id === value) throw new Error('got it')
if (node.nodes && node.nodes.length > 0) {
for (let i = 0; i < node.nodes.length; i++) this.getNodePath(node.nodes[i], tempPath, value)
tempPath.pop()
} else {
tempPath.pop()
}
}