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))