gpt4 book ai didi

javascript - 如前所述将带有文件路径的数组转换为对象

转载 作者:行者123 更新时间:2023-12-01 01:33:00 27 4
gpt4 key购买 nike

我需要一个 javascript 函数将带有文件路径字符串的数组转换为对象,如下所示:

  let files = [
"Folder/file.ext",
"Folder/file2.ext",
"Folder/file3.ext",
"Folder/nestedfolder/file.ext",
"Folder2/file1.ext",
"Folder2/file2.ext",
"file1.ext",
"file2.ext",
"file3.ext",
];

listToTree(files);

它应该输出一个带有对象的数组,如下所示:

[
{
text: "Folder",
children: [
{text: "file.ext"},
{text: "file1.ext"},
{text: "file2.ext"},
{text: "nestedfolder", children: [{text: "file.ext"}]},
]
},
{
text: "Folder2",
children: [
{text: "file1.ext"},
{text: "file2.ext"},
]
},
{text: "file1.ext"},
{text: "file2.ext"},
{text: "file3.ext"}
];

这是我当前正在使用的函数。但还没有完全实现。

function listToTree(files) {
let filestmp = files.map(file => {
if (typeof file === "string") return file;
return file.path
});
let filesl = filestmp.map(fileee => fileToObject(fileee));

return filesl;
}

function fileToObject(filee) {
if (filee.includes("/")) {
// this is a folder
let count = filee.indexOf("/");
return {text: filee.substring(0, count), children: [fileToObject(filee.substring(count + 1))]}
} else {
// this is a file
return {text: filee}
}
}

export default listToTree

它输出:

[ { text: 'Folder', children: [ { text: 'file.ext' } ] },
{ text: 'Folder', children: [ { text: 'file2.ext' } ] },
{ text: 'Folder', children: [ { text: 'file3.ext' } ] },
{ text: 'Folder',
children:
[ { text: 'nestedfolder', children: [ { text: 'file.ext' } ] } ] },
{ text: 'Folder2', children: [ { text: 'file1.ext' } ] },
{ text: 'Folder2', children: [ { text: 'file2.ext' } ] },
{ text: 'file1.ext' },
{ text: 'file2.ext' },
{ text: 'file3.ext' } ]

现在如你所见。每个文件列表数组都有自己的对象。我需要合并位于同一文件夹位置的文件。

最佳答案

将树表示为数组有点不方便,因为每次都需要搜索数组才能找到合适的 Node ,这对于大型数组来说效率很低。一种选择是在一次传递中构建一个树对象,然后进行第二次传递以获取 Object.values。这是一个例子:

let files = ["Folder/file.ext","Folder/file2.ext","Folder/file3.ext","Folder/nestedfolder/file.ext","Folder2/file1.ext","Folder2/file2.ext","file1.ext","file2.ext","file3.ext",];

function addPath(arr, obj = {}){
let component = arr.shift()
let current = obj[component] || (obj[component] = {text:component})
if (arr.length) {
addPath(arr, current.children || (current.children = {}))
}
return obj
}

function makeArray(obj){
let arr = Object.values(obj)
arr.filter(item => item.children).forEach(item => {
item.children = makeArray(item.children)
})
return arr
}

// make tree
let treeObj = files.reduce((obj, path) => addPath(path.split('/'), obj), {})
// convert to array
let arr = makeArray(treeObj)

console.log(arr)

另一种方法是使用 find() ,它可以工作并且可能更容易阅读......但可能效率较低,因为您需要在每次传递时搜索结果数组:

let files = ["Folder/file.ext","Folder/file2.ext","Folder/file3.ext","Folder/nestedfolder/file.ext","Folder2/file1.ext","Folder2/file2.ext","file1.ext","file2.ext","file3.ext",];

function addPath(pathcomponents, arr ){
let component = pathcomponents.shift()
let comp = arr.find(item => item.text === component)
if (!comp) {
comp = {text: component}
arr.push(comp)
}
if(pathcomponents.length){
addPath(pathcomponents, comp.children || (comp.children = []))
}
return arr
}



let res = files.reduce((arr, path) => addPath(path.split('/'), arr), [])

console.log(res)

关于javascript - 如前所述将带有文件路径的数组转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53086927/

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