//根据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])
      }
    }
  }
}
 posted on 2024-04-10 17:16  还能不能行d  阅读(147)  评论(0)    收藏  举报