js 递归树结构数据查找指定元素的所有父级

一、模拟数据

var arrData = [{
    "label": "中国",
    "parentId": null,
    "id": "0",
    "children": [{
            "label": "河北",
            "parentId": "0",
            "id": "1",
            "children": [{
                    "label": "石家庄",
                    "parentId": "1",
                    "id": "1.1",
                    "children": null
                },
                {
                    "label": "保定",
                    "parentId": "1",
                    "id": "1.2",
                    "children": null
                },
                {
                    "label": "邯郸",
                    "parentId": "1",
                    "id": "1.3",
                    "children": [{
                            "label": "邯山区",
                            "parentId": "1.3",
                            "id": "1.3.1",
                            "children": [{
                                "label": "丛西街道",
                                "parentId": "1.3.1",
                                "id": "1.3.1.1",
                                "children": null
                            }]
                        },
                        {
                            "label": "涉县",
                            "parentId": "1.3",
                            "id": "1.3.2",
                            "children": null
                        },
                        {
                            "label": "丛台区",
                            "parentId": "1.3",
                            "id": "1.3.3",
                            "children": null
                        }
                    ]
                }
            ]
        },
        {
            "label": "山东",
            "parentId": "0",
            "id": "2",
            "children": [{
                "label": "济南",
                "parentId": "2",
                "id": "2.1",
                "children": null
            }]
        },
        {
            "label": "北京",
            "parentId": "0",
            "id": "3",
            "children": null
        }

    ]
},];

二、从下往上递归

function findParents(treeData,id){
  let allparents = []
  if(treeData.length==0){
    return 
  }

  let findele = (data,id) => {
    if(!id) return
    data.forEach((item,index) => {
      if(item.id == id){
        allparents.unshift(item.id)
        findele(treeData,item.parentId)

      }else{
        if(!!item.children){
            findele(item.children,id)
        }
        
      }
    })
  }

  findele(treeData,id)
  return allparents

}
console.log('result',findParents(arrData,"1.3.1.1"))

三、从上往下递归

function findParents(treeData,id){
  if(treeData.length==0) return
  for(let i=0;i<treeData.length;i++){
    if(treeData[i].id == id){
      return []
    }else{
      if(treeData[i].children){
        let res = findParents(treeData[i].children,id)
        if(res !== undefined){
          return res.concat(treeData[i].id)
        }
      }
    }
  }  
}

 

上一篇:elementUI树结构添加子级结构


下一篇:实现递归树形结构