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

2023-12-23 18:38:44

业务场景:选中树的末级节点后,拿到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 --

文章来源:https://blog.csdn.net/m0_62323730/article/details/135166336
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。