算法-从树某节点到顶级的id,拼接成字符串

29 篇文章 0 订阅

业务场景:选中树的末级节点后,拿到id,向上找每个层级的父级,把向上的所有层级的id,按照从顶级到末级顺序拼接成字符串 

例如:点击的是home1,id是0010284,

拼接后:‘0010010500105010010284’

[{
    "id": "001",
    "label": "顶级",
    "parentID": "0",
    "isusedic": null,
    "ctrlmode": null,
    "janeSpell": null,
    "children": [
        {
            "id": "00105",
            "label": "管理处",
            "parentID": "001",
            "isusedic": null,
            "ctrlmode": null,
            "janeSpell": null,
            "children": [
                {
                    "id": "0010501",
                    "label": "总部",
                    "parentID": "00105",
                    "isusedic": null,
                    "ctrlmode": null,
                    "janeSpell": null,
                    "children": [
                        {
                            "id": "0010277",
                            "label": "公司",
                            "parentID": "0010501",
                            "isusedic": 1,
                            "ctrlmode": null,
                            "janeSpell": null,
                            "children": [],
                            "nodeType": "10"
                        },
                        {
                            "id": "0010284",
                            "label": "home",
                            "parentID": "0010501",
                            "isusedic": 1,
                            "ctrlmode": null,
                            "janeSpell": null,
                            "children": [],
                            "nodeType": "10"
                        },
                        {
                            "id": "0010285",
                            "label": "home1",
                            "parentID": "0010501",
                            "isusedic": 1,
                            "ctrlmode": null,
                            "janeSpell": null,
                            "children": [],
                            "nodeType": "10"
                        },
                        
                    ],
                    "nodeType": "4"
                }
            ],
            "nodeType": "3"
        },
        {
            "id": "00104",
            "label": "桥管",
            "parentID": "001",
            "isusedic": null,
            "ctrlmode": null,
            "janeSpell": null,
            "children": [
                {
                    "id": "0010401",
                    "label": "网所",
                    "parentID": "00104",
                    "isusedic": null,
                    "ctrlmode": null,
                    "janeSpell": null,
                    "children": [
                        {
                            "id": "0010184",
                            "label": "天坛站",
                            "parentID": "0010401",
                            "isusedic": 1,
                            "ctrlmode": null,
                            "janeSpell": null,
                            "children": [],
                            "nodeType": "10"
                        },
                        {
                            "id": "0010185",
                            "label": "地坛站",
                            "parentID": "0010401",
                            "isusedic": 1,
                            "ctrlmode": null,
                            "janeSpell": null,
                            "children": [],
                            "nodeType": "10"
                        }
                       
                    ],
                    "nodeType": "4"
                }
            ],
            "nodeType": "3"
        },
        
    ],
    "nodeType": "6"
},
{},
{}
]

// 查找上级层级选中的节点的父节点id拼接字符串
      findParentIds(data, id, result = []) {
        for (const item of data) {
          if (item.id === id) {
            result.push(item.id);
            return true;
          }
          if (item.children && item.children.length > 0) {
            if (findParentIds(item.children, id, result)) {
              result.push(item.id);
              return true;
            }
          }
        }
        return false;
      },
      getParentIdsString(data, id) {
        const parentIds = [];
        findParentIds(data, id, parentIds);
        if (parentIds.length === 0) {
          return "ID not found";
        }
        const idsString = parentIds.reverse().join("");
        return idsString;
      },

      const nodeCode = getParentIdsString(merSupertOptions, node) // 参数(树的数组对象,选择的节点id)
      console.log(nodeCode, '--- 拼接后的nodeCode --') // 0010010500105010010284, '--- 拼接后的nodeCode --

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值