递归构建树形结构

// 后台数据
[
            {
                "Id": "1699614694899712000",
                "OrganizationName": "1",
                "SuperiorOrganization": "2",
            },
            {
                "Id": "1699614724377280511",
                "OrganizationName": "2",
                "SuperiorOrganization": "3",
            },
            {
                "Id": "1699614825288040442",
                "OrganizationName": "3",
            },
            {
                "Id": "1699716853951627263",
                "OrganizationName": "4",
                "SuperiorOrganization": "2",
            },
            {
                "Id": "1699726918335070204",
                "OrganizationName": "5",
            }, {
                "Id": "1699614694899712005",
                "OrganizationName": "6",
                "SuperiorOrganization": "4",
            },
            {
                "Id": "1699614724377280516",
                "OrganizationName": "7",
                "SuperiorOrganization": "4",
            },
            {
                "Id": "1699614825288040447",
                "OrganizationName": "8",
            },
            {
                "Id": "1699716853951627268",
                "OrganizationName": "9",
                "SuperiorOrganization": "5",
            },
            {
                "Id": "1699726918335070209",
                "OrganizationName": "10",
                "SuperiorOrganization": "5",
            }
        ]

// 最终要实现的数据结构
 const nodes = [
             {
                 title: 3,
                 key: "1699614825288040442",
                 children: [
                     {
                         title: 2,
                         key: "1699614724377280511",
                         children: [
                             {
                                 title: 1,
                                 key: "1699614694899712000"
                             }, {
                                 title: 4,
                                 key: "1699716853951627263",
                                 children: [
                                     {
                                         title: 6,
                                         key: "1699614694899712005"
                                     }, {
                                         title: 7,
                                         key: "1699614724377280516"
                                     },
                                 ]
                             },
                         ]
                     }
                 ]
             },
             {
                 title: 5,
                 key: "1699726918335070204",
                children: [
                     {
                         title: 9,
                         key: "1699716853951627268"
                     }, {
                         title: 10,
                         key: "1699726918335070209"
                     },
                 ]
             },
             {
                 title: 8,
                 key: "1699614825288040447",
             }
         ]
         
// 递归构建树形结构
buildTree(list, parentOrganization) {
        const tree = [];    // 定义空数组,存储最终的树形结构数据
        for (const organization of list) {    // 遍历后台list数据
            if (organization.ParentOrganization === parentOrganization) {    // 取含相同属性值的项,如果相同作为子进行递归
                const node = {
                    title: `${organization.Name} [${organization.OrganizationCode}]`,
                    name: organization.Name,
                    key: organization.Id,
                };
                const children = this.buildTree(list, organization.Name);
                if (children.length > 0) {
                    // @ts-ignore
                    node.children = children;
                } else {
                    // @ts-ignore
                    node.isLeaf = true;
                }
                tree.push(node);
            }
        }
        return tree;
    }
    
    this.nodes = this.buildTree(res.NgpDataSet.T_Result, undefined);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值