前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何将路径字符串数组(string[])转成树结构(treeNode[])?

如何将路径字符串数组(string[])转成树结构(treeNode[])?

作者头像
唐志远
发布2023-08-01 20:10:14
1820
发布2023-08-01 20:10:14
举报
文章被收录于专栏:FE32 CodeFE32 Code

需求

这里的UI使用的是Element-Plus

将一个路径字符串数组(当然也可能是其他目标字符串数组),渲染成

代码语言:javascript
复制
/*

source:
  /a/b/c/d/e
  /a/b/e/f/g
  /a/b/h
  /a/i/j
  /a/i/k

what I need:
       a
     /   \
    b     i
   /|\   / \
  c e h j   k
  | |
  d f
  | |
  e g   

*/

这里模拟了待转化的字符串数组如下:

代码语言:javascript
复制
let TargetKeyLists = [
    "D:\\$RECYCLE.BIN\\S-1-5-21-2980625316-768050560-104202119-1001\\$I0KVI2C.css",
    "D:\\$RECYCLE.BIN\\S-1-5-21-2980625316-768050560-104202119-1001\\$I61JY0M.php",
    "D:\\$RECYCLE.BIN\\S-1-5-21-2980625316-768050560-104202119-1001\\$I8IC15E.html",
    "D:\\$RECYCLE.BIN\\S-1-5-21-2980625316-768050560-104202119-1001\\$I9UTNI9.ico",
    "D:\\Program Files\\Sandboxie",
    "D:\\fbs\\xampp-windows-x64-8.2.0-0-VS16-installer.exe",
    "D:\\fcstor\\.svn",
    "D:\\xampp\\MercuryMail",
    "D:\\xampp\\anonymous",
    "D:\\xampp\\apache",
    "C:\\$Recycle.Bin\\S-1-5-18",
    "C:\\$Recycle.Bin\\S-1-5-21-2980625316-768050560-104202119-1001",
    "C:\\$Recycle.Bin\\S-1-5-21-2980625316-768050560-104202119-500",
    "C:\\BOOTNXT",
]

转化后的目标结构如下:

代码语言:javascript
复制
[
    {
        "label": "D:",
        "children": [
            {
                "label": "$RECYCLE.BIN",
                "children": [
                    {
                        "label": "S-1-5-21-2980625316-768050560-104202119-1001",
                        "children": [
                            {
                                "label": "$I0KVI2C.css",
                                "children": []
                            },
                            {
                                "label": "$I61JY0M.php",
                                "children": []
                            },
                            {
                                "label": "$I8IC15E.html",
                                "children": []
                            },
                            {
                                "label": "$I9UTNI9.ico",
                                "children": []
                            }
                        ]
                    }
                ]
            },
            {
                "label": "Program Files",
                "children": [
                    {
                        "label": "Sandboxie",
                        "children": []
                    }
                ]
            },
            {
                "label": "fbs",
                "children": [
                    {
                        "label": "xampp-windows-x64-8.2.0-0-VS16-installer.exe",
                        "children": []
                    }
                ]
            },
            {
                "label": "fcstor",
                "children": [
                    {
                        "label": ".svn",
                        "children": []
                    }
                ]
            },
            {
                "label": "xampp",
                "children": [
                    {
                        "label": "MercuryMail",
                        "children": []
                    },
                    {
                        "label": "anonymous",
                        "children": []
                    },
                    {
                        "label": "apache",
                        "children": []
                    }
                ]
            }
        ]
    },
    {
        "label": "C:",
        "children": [
            {
                "label": "$Recycle.Bin",
                "children": [
                    {
                        "label": "S-1-5-18",
                        "children": []
                    },
                    {
                        "label": "S-1-5-21-2980625316-768050560-104202119-1001",
                        "children": []
                    },
                    {
                        "label": "S-1-5-21-2980625316-768050560-104202119-500",
                        "children": []
                    }
                ]
            },
            {
                "label": "BOOTNXT",
                "children": []
            }
        ]
    }
]

步骤

1.在utils文件夹下新建index.ts文件。

代码语言:javascript
复制
interface TreeNode {
    label: string
    children: TreeNode[]
}

// 循环构建子节点
const buildChildrenNode = (children: TreeNode[], nodeArray: string[]) => {
    for (let i in nodeArray) {
        let _i: number = Number(i)
        let node: TreeNode = {
            label: nodeArray[_i],
            children: []
        }
        if (_i != nodeArray.length) {
            node.children = []
        }
        if (children.length == 0) {
            children.push(node)
        }
        let isExist = false
        for (let j in children) {
            if (children[j].label == node.label) {
                if (_i != nodeArray.length - 1 && !children[j].children) {
                    children[j].children = []
                }
                children = _i == nodeArray.length - 1 ? children : children[j].children
                isExist = true
                break
            }
        }
        if (!isExist) {
            children.push(node)
            if (_i != nodeArray.length - 1 && !children[children.length - 1].children) {
                children[children.length - 1].children = []
            }
            children = _i == nodeArray.length - 1 ? children : children[children.length - 1].children
        }
    }
}
/**
 * @description: string[] ->  treeNode[]
 * @param {string} list 资源路径数组
 * @param {string} clientLabel 是否需要在最外面还要套一层(exam:所属客户端)
 * @return { treeNode[] }
 */
export const array2Tree = (list: string[], clientLabel?: string) => {
    let targetList: TreeNode[] = []
    list.map(item => {
        let label = item
        let nodeArray: string[] = label.split('\\').filter(str => str != '')
        // 递归
        let children: TreeNode[] = targetList
        // 构建根节点
        if (children.length == 0) {
            let root: TreeNode = {
                label: nodeArray[0],
                children: []
            }
            if (nodeArray.length > 1) {
                root.children = []
            }
            children.push(root)
            buildChildrenNode(children, nodeArray)
        } else {
            buildChildrenNode(children, nodeArray)
        }
    })
    if (!clientLabel) {
        return targetList
    } else {
        const newArr = [{
            label: clientLabel,
            children: targetList
        }]
        return newArr
    }
}

2.在目标页面中引入并调用array2Tree方法即可。

代码语言:javascript
复制
<template>
    <el-tree :data="confirmTreeList" default-expand-all />
</template>
代码语言:javascript
复制
import { array2Tree } from '@/utils/index'

let confirmTreeList: TreeNode[] = []

confirmTreeList = array2Tree(TargetKeyLists)

3.效果如下:

这里说明一下,array2Tree()方法中的clientLabel参数其实可要可不要,也可继续扩展,根据自身业务而定。

比如我想最后实现的效果如下图所示:

所以在第2步中传入clientLabel即可:

代码语言:javascript
复制
confirmTreeList = array2Tree(TargetKeyLists,'test(192.168.0.213)')
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-07-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求
  • 步骤
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档