树结构:通过id找父节点、通过某个节点获取完整路径

树形结构的数据是在工作当中经常使用到的,下面实例几个比较会频繁遇到的需求,在处理时,基本上都需要通过递归实现。

1、通过某个节点的id,找出其所有的父节点

2、通过某个节点的id,找出他所有的子节点

3、通过某个节点的id,获取其父级名称(或其他)的完整路径信息

4、通过某个节点的id,获取对应节点的完整信息

/**
 * 1、根据节点id,获取其所有父节点
 * @param {*} list 完整的树结构数组
 * @param {*} id 当前点击的id
 * @param {*} name 需要对比的id 的属性节点
 * @param {*} child 子节点名称
 * @returns 
 */
getAllParentArr(list,id,name,child){
    for(let i in list){
        if(list[i][name] == id){
            return [list[i]]
        }
        if (list[i][child]) {
            let node = this.getAllParentArr(list[i][child],id,name,child)
            if(!!node){
                return node.concat(list[i])
            }
        }
     }
 }
/**
 * 2、通过某个节点的id,找出他所有的子节点
 * @param {*} list 当前点击节点的数据(不是总的树结构)
 * @param {*} arr 返回的数组  默认为 []
 * @returns 
 */
getChildIds(list = [], arr = []) {
    for (let item of list ) {
        arr.push(item.id);
        if (item.children && item.children.length) {
          this.getChildIds(item.children, arr);
        }
    }
    return arr;
},
/**
 * 3、通过某个节点的id,获取其父级名称(或其他)的完整路径信息
 * @param {*} tree 树结构数组
 * @param {*} value 当前节点的id
 * @returns 
 */
getItemByIdInTree( tree,value,path=""){
   for(var i=0;i<tree.length;i++){
     let tempPath = path
     tempPath =  `${tempPath ? tempPath + '/' : tempPath}${tree[i].name}` // 避免出现在最前面的/
     if(tree[i].id == value){
        return tempPath
     } else if(tree[i].children){
         let reuslt = this.getItemByIdInTree(tree[i].children,value,tempPath)
         if(reuslt){
             return reuslt
         }
     }
   }
},

其实根据id获取树节点的完整信息和第三个方法类似,只需要在返回数据做处理即可

/**
 * 4、通过某个节点的id,获取该节点的完整信息
 * @param {*} tree 树结构数组
 * @param {*} value 当前节点的id
 * @returns 
 */
getItemByIdInTree( tree,value){
   for(var i=0;i<tree.length;i++){
     let item = tree[i]
     if(tree[i].id == value){
        return item
     } else if(tree[i].children){
         let reuslt = this.getItemByIdInTree(tree[i].children,value)
         if(reuslt){
             return reuslt
         }
     }
   }
},

 目前只用过这些,如果有再遇到新的需求会继续补充

有问题欢迎指出

  • 4
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用递归方式来实现获取数组树结构中某个节点及其所有的父节点: ```javascript function findNodeById(tree, id, path = []) { for (let i = 0; i < tree.length; i++) { const node = tree[i]; if (node.id === id) { // 到目标节点,返回路径 return [...path, node]; } else if (node.children) { // 递归遍历节点 const result = findNodeById(node.children, id, [...path, node]); if (result) { // 到目标节点,返回路径 return result; } } } // 没有到目标节点 return null; } ``` 这个函数接受三个参数: - `tree`:树结构数组 - `id`:要查节点ID - `path`:当前路径数组,用于记录最终路径 如果到了目标节点,就返回路径数组;如果没有到,就返回 `null`。 使用示例: ```javascript const tree = [ { id: 1, name: '节点1', children: [ { id: 2, name: '节点2', children: [ { id: 3, name: '节点3', }, { id: 4, name: '节点4', }, ], }, { id: 5, name: '节点5', }, ], }, { id: 6, name: '节点6', children: [ { id: 7, name: '节点7', children: [ { id: 8, name: '节点8', }, { id: 9, name: '节点9', }, ], }, ], }, ]; const path = findNodeById(tree, 4); console.log(path); // 输出:[ { id: 1, name: '节点1', ... }, { id: 2, name: '节点2', ... }, { id: 4, name: '节点4', ... } ] ``` 这个示例中,我们调用 `findNodeById` 函数,查 ID 为 4 的节点,并返回其路径数组。最终输出结果为 `[ { id: 1, name: '节点1', ... }, { id: 2, name: '节点2', ... }, { id: 4, name: '节点4', ... } ]`,表示从根节点开始,依次经过节点 1、节点 2 和节点 4,才能到达目标节点

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值