@
doommm function markFolder(tree) {
tree.forEach(item => {
if (item.children) {
item.children.forEach(child => {
if ("children" in child) {
item.hasFolderChild = true;
}
});
markFolder(item.children);
}
});
}
function addPath(folders) {
folders.forEach(item => {
item.isOpen = true;
item.checked = false;
item.folder_name = item.folderName;
if ("children" in item) {
item.children.forEach(child => {
child.path = item.path + "/" + child.folderName;
});
addPath(item.children);
}
});
}
function makeTree(data,length = 0) {
let { files, folders } = data;
const rootName = files[0].file_folder.slice(1).split("/")[0];
folders.forEach(item => {
item.path = "/" + rootName + "/" + item.folderName;
});
addPath(folders);
let tree = [{ children: folders }];
tree[0].folderName = rootName;
tree[0].folder_name = rootName;
tree[0].path = "/" + rootName;
tree[0].isOpen = true;
files.forEach(item => {
console.count(item.file_folder)
let pathArr = item.file_folder.slice(length + 1).split("/");
let curr = {}; //指针对象
pathArr.forEach((path, index) => {
if (index === 0) {
let root = tree.find(
branch => branch.folderName === path || branch.folder_name === path
);
if (!root) {
tree.push({
folder_name: path,
children: [],
isOpen: true,
root: "root",
path: "/" + path,
checked: false
});
}
curr = tree.find(
branch => branch.folderName === path || branch.folder_name === path
);
} else {
let obj = curr.children.find(
branch => branch.folderName === path || branch.folder_name === path
);
if (!obj) {
curr.children.push({
folder_name: path,
children: [],
isOpen: true,
path: curr.path + "/" + path,
checked: false
});
}
curr = curr.children.find(
branch => branch.folderName === path || branch.folder_name === path
);
}
});
curr.children.push(
Object.assign(
{
path: item.file_folder,
checked: false
},
item
)
);
});
markFolder(tree);
return tree;
}
不光是处理树的代码 还有业务代码 工具代码