递归

const treeData=[{
id:1,parentId:null,name:"节点1",children:[{ id:11,parentId:1,name:"节点1-1" }]
},
{
id:2,parentId:null,name:"节点2",children:[{ id:22,parentId:2,name:"节点2-1" }]
}]

function getTreeData(){
  let map = [];
  let treeList = [];
  recursion(treeData, map);
  console.log(map);
  TreeToList(treeData, treeList);
  console.log(treeList);
}


/**
 * 获取tree中节点的 id 和 parentId
 * @param {*} treeData tree
 * @param {*} map 保存id和parentId的map
 */
function recursion(treeData,map){
  treeData.forEach(tree=>{
    map.push({id:tree.id, parentId:tree.parentId})
    if(tree.children){
      recursion(tree.children, map)
    }
  })
}


/**
 * tree数据格式转换为list 
 * @param treeData Tree 原tree数据
 * @param listData Array 转换后的list数据
 */
function TreeToList(treeData, listData){
  treeData.forEach(tree=>{
    // 拷贝当前对象,删除其 children 属性
    let obj = Object.assign({},tree)
    delete obj.children
    listData.push(obj)
    
    if(tree.children){
      TreeToList(tree.children, listData)
    }
  })
}
通过当前层级的id 方向获取所有的 父层级的 label 内容
const dataTree = [
{ 
id: "1", label: "label1",
children: [
  {id: "11", label: "label11"},
  {id: "12", label: "label12",
  children: [
    {id: "121", label: "label121"},
    {id: "122", label: "label122"},
   ]},
 ]},
{ 
id: "2", label: "label2",
children: [
  {id: "21", label: "label21"},
  {id: "22", label: "label22"},
]},
]
function handleAopLog(){
  aopLog(dataTree , "121")
},

function aopLog(treeData,nodeId){
  const treeList = []
  const idList = []
  treeToList(treeData, treeList, idList)
  let levelTitle = []
  levelTitle.push(getLabel(treeList,nodeId))
  recursion(idList, treeList, nodeId, levelTitle)
  levelTitle = levelTitle.join("》")   
},

 // 递归获取每层的 label
function recursion(idList, treeList, id, levelTitle){
  const parentId = getId(idList, id)
  if(!parentId) return
  levelTitle.unshift(getLabel(treeList, parentId))
  recursion( idList, treeList, parentId, levelTitle )
},
 
function getId(idList, id){
  return idList.find(item =>item.id === id).parentId
},

function getLabel(treeList, id){
  return treeList.find(tree =>tree.id === id).label
},

/**
  * tree 数据格式转换为 list 拼接 [{id: id, parentId:parentId }] 数组
  * 当treeData 数据中没有 parentId 时,手动添加 parentId 数据
  * @param treeData Tree 原tree数据
  * @param dataList Array 转换后的 list 数据
  * @param idList Array 转换后的 id 数据
  */
 function treeToList(treeData, dataList, idList, parentId=null){
   treeData.forEach(tree=>{
     // 拷贝当前对象,删除其 children 属性
     let obj = Object.assign({},tree)
     delete obj.children
     if(!obj.parentId) obj["parentId"] = parentId
     dataList.push(obj)
     idList.push({ id: obj.id, parentId: parentId })
     if(!tree.children || tree.children.length===0) return
     treeToList(tree.children, dataList, idList, tree.id)
   })
 },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值