javascript根据目录路径生成目录结构

1.要生成的大概结构:

var treeData = [
        {
            key: '0',
            title: 'test',
            children: [
                {
                    key: '1',
                    title: 'cmp',
                    children: [
                        { key: '3', title: 'b.txt' },
                    ],
                },
                { key: '2', title: 'a.txt' },
            ],
        },
        {
            key: '4',
            title: 'demo',
            children:[
                { key: '5', title: 'b.txt' },
            ]
        }
    ];

2.数据:

 const arr = ['test/', 'test/a.txt', 'test/cmp/', 'test/cmp/b.txt', 'demo/', 'demo/b.txt']

3.代码实现:

const arr = ['test/', 'test/a.txt', 'test/cmp/', 'test/cmp/b.txt', 'demo/', 'demo/b.txt']
    // 不考虑root:"/";
    // 文件名中不存在"/"
    // 同层级中,目录和文件不可同名
    const treeify = (nodeList) => {
        const root = {
            key: 1,
            children: []
        };
         // 生成节点信息按层级
        const nodeInLevel = []
        nodeList.forEach(t => {
            const pathInfoList = t.split('/');
            let isDir = true
            if (pathInfoList[pathInfoList.length - 1]) {
                isDir = false
            }
            const key = (Math.random() + '').split('.')[1] * 1
            const title = isDir ? pathInfoList[pathInfoList.length - 2] : pathInfoList[pathInfoList.length - 1]
            const level = isDir ? pathInfoList.length - 2 : pathInfoList.length - 1
            // collet it
            nodeInLevel[level] = nodeInLevel[level] || []
            nodeInLevel[level].push({
                _originPath: t,
                _pathInfo: pathInfoList,
                _patName: level === 0 ? '/' : isDir ? pathInfoList[pathInfoList.length - 3] : pathInfoList[pathInfoList.length - 2],
                isDir,
                children: isDir ? [] : null,
                key,
                title,
            })
        });
        // 广度排,按每层级
        const getNodeCot = (node, level, root) => {
            const {_patName} = node;
            let curCot = null;
            if (level === 0) {
                curCot = root.children
            } else {
                const pat = nodeInLevel[level - 1].find(t => t.isDir && t.title === _patName);
                if (!pat) {
                    throw new Error(
                        `Node ${node._originPath} cant find parent ${_patName}`,
                    );
                }
                return pat.children
            }
            return curCot
        };
        const maxLevel = nodeInLevel.length;
        for (let level = 0; level < maxLevel; level++) {
            nodeInLevel[level].forEach(node => {
                const curCot = getNodeCot(node, level, root);
                curCot.push(node)
            })
        }
        return root;
    };
    const r = treeify(arr);
    console.log(r)

6.如果只返回文件路径,不返回目录路径,比如这样:

 

 const newArr = ['test/data/aa.html', 'test/ddd/a.txt', 'test/cmp/ccc.txt', 'test/cmp/b.txt', 'demo/ccc/tttt/demo.html', 'demo/b.txt']

7.写一个方法解析出目录路径

 function getCatalogPath(nodeList) {
    let newNodeList = [];
    for (let i = 0; i < nodeList.length; i++) {
      let pathNameArr = nodeList[i].split('/');
      let pathNameCatalog = '';
      if (pathNameArr.length > 1) {
        for (let y = 0; y < pathNameArr.length; y++) {
          if (pathNameArr[pathNameArr.length - 1]) {
            if (y === pathNameArr.length - 1) {
              pathNameCatalog = pathNameCatalog + pathNameArr[y];
            } else {
              pathNameCatalog = pathNameCatalog + pathNameArr[y] + '/';
            }
            let flag = false;
            if (newNodeList.length > 0) {
              for (let d = 0; d < newNodeList.length; d++) {
                if (newNodeList[d] === pathNameCatalog) {
                  flag = true;
                  break;
                }
              }
              if (!flag) {
                newNodeList.push(pathNameCatalog);
              }
            } else {
              newNodeList.push(pathNameCatalog);
            }
          }
        }
      } else {
        newNodeList.push(nodeList[i]);
      }
    }
    return newNodeList;
  }
console.log(getCatalogPath(newArr))

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值