在纯 Javascript 中从 Url 生成 Treeview
Generate Treeview from Url in pure Javascript
我正在以数组的形式获取 Url 个文件,如下所示
我想实现这样的目标
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:[......]
}
]
}]
}
]
}
]
.......
如果你能帮忙,请告诉我。
顺便说一句,我想实现如下所示
如果您能提出更好的建议,那将对我有所帮助..
您需要执行一些 字符串解析 以将 URL 字符串分成不同的部分,收集创建树状结构所需的所有信息。
基本上,您可以将所有 URL 个字符串拆分成它们的部分,并通过分析 URL 个字符串的所有子部分来创建最终数据结构。
let urls = [
'http://host.com/Performance/excel doc 1.xlsx',
'http://host.com/BusinessUnits/Administration/AlKhorDocument/Album1/...',
// ...
];
let result = [];
urls.forEach(url => {
let relevantUrl = url.replace('http://host.com/', ''); // remove the unnecessary host name
let sections = relevantUrl.split('/'); // get all string sections from the URL
sections.forEach(section => {
// check if that inner object already exists
let innerObject = result.find(obj => obj.name === section);
if(!innerObject) {
// create an inner object for the section
innerObject = {
name: section,
files: [],
folders: []
};
}
// add the current URL section (as object) to the result
result.push(innerObject);
});
});
您还需要处理的是保存部分对象的当前子级别,您可以这样做 either iteratively or by calling a recursive function。
通过只包含字符串的路径部分,您可以通过减少文件夹结构来减少数组并将最终文件添加到文件夹结构。
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; }
我正在以数组的形式获取 Url 个文件,如下所示
我想实现这样的目标
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:[......]
}
]
}]
}
]
}
]
....... 如果你能帮忙,请告诉我。
顺便说一句,我想实现如下所示
如果您能提出更好的建议,那将对我有所帮助..
您需要执行一些 字符串解析 以将 URL 字符串分成不同的部分,收集创建树状结构所需的所有信息。
基本上,您可以将所有 URL 个字符串拆分成它们的部分,并通过分析 URL 个字符串的所有子部分来创建最终数据结构。
let urls = [
'http://host.com/Performance/excel doc 1.xlsx',
'http://host.com/BusinessUnits/Administration/AlKhorDocument/Album1/...',
// ...
];
let result = [];
urls.forEach(url => {
let relevantUrl = url.replace('http://host.com/', ''); // remove the unnecessary host name
let sections = relevantUrl.split('/'); // get all string sections from the URL
sections.forEach(section => {
// check if that inner object already exists
let innerObject = result.find(obj => obj.name === section);
if(!innerObject) {
// create an inner object for the section
innerObject = {
name: section,
files: [],
folders: []
};
}
// add the current URL section (as object) to the result
result.push(innerObject);
});
});
您还需要处理的是保存部分对象的当前子级别,您可以这样做 either iteratively or by calling a recursive function。
通过只包含字符串的路径部分,您可以通过减少文件夹结构来减少数组并将最终文件添加到文件夹结构。
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; }