//根据id查询该节点名称
function getNameById(list, id) {
for (let i in list) {
if (list[i].id === id) {
return list[i].name
}
if (list[i].children && list[i].children.length > 0) {
const name = getNameById(list[i].children, id)
if (name) {
return name
}
}
}
}
//根据id查询该节点和所有父级节点
function getParentsById(list, id) {
for (let i in list) {
if (list[i].id === id) {
//查询到就返回该数组对象
return [list[i]]
}
if (list[i].children) {
let node = getParentsById(list[i].children, id)
if (node !== undefined) {
//查询到把父节点连起来
return node.concat(list[i])
}
}
}
}