前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js 获取多级路径

js 获取多级路径

作者头像
tianyawhl
发布2022-09-28 13:32:29
33.4K0
发布2022-09-28 13:32:29
举报
文章被收录于专栏:前端之攻略前端之攻略

数据结构 

代码语言:javascript
复制
 let treeData = [{
      id: 1,
      label: '一级 1',
      children: [{
        id: 4,
        label: '二级 1-1',
        children: [{
          id: 9,
          label: '三级 1-1-1'
        }, {
          id: 10,
          label: '三级 1-1-2'
        }]
      }]
    }, {
      id: 2,
      label: '一级 2',
      children: [{
        id: 5,
        label: '二级 2-1'
      }, {
        id: 6,
        label: '二级 2-2'
      }]
    }, {
      id: 3,
      label: '一级 3',
      children: [{
        id: 7,
        label: '二级 3-1'
      }, {
        id: 8,
        label: '二级 3-2'
      }]
    }];
代码语言:javascript
复制
const findNodeInTree = (data, key, callback) => {
      for (let i = 0; i < data.length; i++) {
        if (data[i].label == key) {
          return callback(data[i], i, data)
        }
        if (data[i].children) {
          findNodeInTree (data[i].children, key, callback)
        }
      }
    }
  
    // 所查找到的节点要存储的方法
    let ObjResult={}
    findNodeInTree(treeData, "二级 1-1", (item, index, arr) => {
      ObjResult== item
    })
  
    // 此时就是ObjResult=对应的要查找的节点
    console.log(ObjResult) 

第二个例子

代码语言:javascript
复制
let res = [{
  name:"一级-1",
  children:[{
    name:"二级-1",
    children:[{
      name:"三级-1-1",
      children:[{
        name:"四级-1-1-1"
      }]
    },{
      name:"三级-1-2",
      children:[{
        name:"四级-1-2-1"
      }]
    }]
  },{
    name:"二级-2",
    children:[{
      name:"三级-2-1",
      children:[{
        name:"四级-2-1-1"
      }]
    },{
      name:"三级-2-2",
      children:[{
        name:"四级-2-2-1"
      }]
    }]
  }]
}]   
代码语言:javascript
复制
function treeFindPath(tree,func,path=[]){
  if(!tree) return[]
  for(let data of tree){
    path.push(data.name)
    console.log(path)
    if(func(data)){
       return path
    } 
    if(data.children){
      const findChildren = treeFindPath(data.children,func,path)
      console.log(findChildren,"children")
      if(findChildren.length) return findChildren
    }
     
    path.pop()
    // console.log(path)
  }
  return []
}
let result = treeFindPath(res,data=>data.name=='四级-2-2-1')
console.log(result)

此例子的另一种方式

代码语言:javascript
复制
let testResult
function treeFindPath(tree,func,path=[]){
  if(!tree) return[]
  for(let data of tree){
    path.push(data.name)
    console.log(path)
    if(func(data)){
        let path1 = JSON.stringify(path || [])
        testResult = JSON.parse(path1)
        // return path
    } 
    if(data.children){
      const findChildren = treeFindPath(data.children,func,path)
      // console.log(findChildren,"children")
      // if(findChildren.length) return findChildren
    }
     
    path.pop()
  }
}
let result = treeFindPath(res,data=>data.name=='四级-2-2-1')
console.log(testResult)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档