gpt4 book ai didi

javascript - 用纯 Javascript 从 Url 生成 Treeview

转载 作者:行者123 更新时间:2023-12-04 10:45:47 25 4
gpt4 key购买 nike

我以如下数组的形式获取文件的 URL

enter image description here

我想实现这样的目标

var mainObj = [
{
name: "Home",
files: ["excel doc 1.xlsx", "excel doc 2.xlsx"],
folders: [{
name: "Procedure",
files: ["excel doc 2.xlsx"],
folders: []
}],
},
{
name: "BusinessUnits",
files: [],
folders:[
{
name:"Administration",
files:[],
folders:[{
name: "AlKhorDocument",
files: [],
folders:[
{
name: "Album1",
files: [],
folders:[......]
}
]
}]
}
]
}

]

......
如果你能帮忙,请告诉我。

顺便说一句,我想在下面实现

enter image description here

如果你能提出更好的建议,那么它会帮助我..

最佳答案

通过只有字符串的路径部分,您可以通过减少文件夹结构并将最终文件添加到文件夹结构来减少数组。

var data = ['Home/excel doc 1.xlsx', 'Home/excel doc 2.xlsx', 'Home/Procedure/excel doc', 'Home/Procedure/2.xlsx', 'BusinessUnits/Administration/AlKhorDocument/Album1/text.txt'],
result = data.reduce((r, p) => {
var path = p.split('/'),
file = path.pop(),
final = path.reduce((o, name) => {
var temp = (o.folders = o.folders || []).find(q => q.name === name);
if (!temp) o.folders.push(temp = { name });
return temp;
}, r);

(final.files = final.files || []).push(file);
return r;
}, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 用纯 Javascript 从 Url 生成 Treeview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59717282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com