其他分享
首页 > 其他分享> > 递归 --获取层级树目标节点及其上层节点

递归 --获取层级树目标节点及其上层节点

作者:互联网

目标,通过传入id,获取到目标点id及其上层节点。返回数组,包含从顶级节点到目标节点所有层级的id。

1、层级结构

2、代码

function getCodingByPage (menus, page, selList) {
            for(let i = 0; i<menus.length; i++) {
                selList.push(menus[i].id)
                if(menus[i]['id'] == page) return selList
                let tempList;
                if(menus[i]['children'].length) tempList = getCodingByPage(menus[i]['children'], page, selList)
                if(tempList) return tempList 
                else selList.pop()
            }
        }

        let menusList = getCodingByPage(JSON.parse(menu), '7537146ca51044c3990726febdc8e435' , [])
        console.log('我是文档信息', menusList)

3、JSON数组demo

let menu = `[
            {
                "title":"消息中心",
                "parentID":"0",
                "sequenceNo":6,
                "children":[
                    {
                        "title":"我的消息",
                        "parentID":"09fe8df41c2943c3ace7d9c9ac90a221",
                        "sequenceNo":1,
                        "children":[],
                        "id":"ff41302ef5e74030becf344ffdf7f46a",
                        "page":"/manage/todo"
                    }
                ],
                "id":"09fe8df41c2943c3ace7d9c9ac90a221",
                "page":""
            },
            {
                "title":"监管企业",
                "parentID":"0",
                "sequenceNo":11,
                "children":[
                    {
                        "title":"基本情况",
                        "parentID":"dbd9b85b30d34f16a2126d6a238ad42f",
                        "sequenceNo":2,
                        "children":[],
                        "id":"7946e35fed5341c588cac8e1ac0c6281",
                        "page":"/manage/iframe?url=http://120.221.72.111:8082/data-reportTest/#/enterprise-information/chengshijianshe/91370281MA3R3BXL5Y"
                    }
                ],
                "id":"dbd9b85b30d34f16a2126d6a238ad42f",
                "page":""
            },
            {
                "title":"三重一大",
                "parentID":"0",
                "sequenceNo":12,
                "children":[
                    {
                        "title":"重大决策事项",
                        "parentID":"b33247b3c0a04d118d534a5402ff7942",
                        "sequenceNo":1,
                        "children":[],
                        "id":"72da243a900d4ddb85677cc9c5cb4ed1",
                        "page":"/manage/threeMatter?categoryID=d233da51ef514ae0bea0e338011fd295"
                    },
                    {
                        "title":"重大人事任免",
                        "parentID":"b33247b3c0a04d118d534a5402ff7942",
                        "sequenceNo":2,
                        "children":[],
                        "id":"fb98e55d2c264a819e1d4e8cd0496b2d",
                        "page":"/manage/threeMatter?categoryID=bc7dc8931d364f719aa412d48764dcc6"
                    },
                    {
                        "title":"重大项目安排",
                        "parentID":"b33247b3c0a04d118d534a5402ff7942",
                        "sequenceNo":3,
                        "children":[],
                        "id":"7537146ca51044c3990726febdc8e435",
                        "page":"/manage/threeMatter?categoryID=72d5cc29136045e78a7a7a12ebbdafab"
                    },
                    {
                        "title":"大额资金运作",
                        "parentID":"b33247b3c0a04d118d534a5402ff7942",
                        "sequenceNo":4,
                        "children":[],
                        "id":"2821e575e7a54a149240a8535f735512",
                        "page":"/manage/threeMatter?categoryID=781ed806de60496cad781ff40b6987ac"
                    },
                    {
                        "title":"决策制度",
                        "parentID":"b33247b3c0a04d118d534a5402ff7942",
                        "sequenceNo":5,
                        "children":[],
                        "id":"9890ba5a65144737b1ba0b2e35280d8a",
                        "page":"/manage/threeDecision"
                    },
                    {
                        "title":"决策会议",
                        "parentID":"b33247b3c0a04d118d534a5402ff7942",
                        "sequenceNo":6,
                        "children":[],
                        "id":"02d14a55393345388c350de3d5bdb552",
                        "page":"/manage/threeMeeting"
                    },
                    {
                        "title":"组织实施",
                        "parentID":"b33247b3c0a04d118d534a5402ff7942",
                        "sequenceNo":7,
                        "children":[],
                        "id":"dfa7fac7b1544edab2979afa94ce4168",
                        "page":"/manage/threeOrganize"
                    }
                ],
                "id":"b33247b3c0a04d118d534a5402ff7942",
                "page":""
            }
        ]`
        console.log('json', JSON.parse(menu))

标签:层级,title,--,sequenceNo,children,page,parentID,id,节点
来源: https://blog.csdn.net/aaron9185/article/details/121241305