[英]Flattening an array of objects javascript
I have the folders in a json file as follows:我在 json 文件中有如下文件夹:
[{ "name": "Knowledge Base",
"files": [
{
"name": "Documents",
"files": [
{
"name": "Quarterly Results"
}
]
},
{
"name": "Favourites",
"files": [
{
"name": "Brawl Stars",
"files": [
{
"name": "NS dying in 5 seconds"
},
{
"name": "Josiah raping NS"
}
]
},
{
"name": "Coding",
"files": [
{
"name": "Coding is so fun"
},
{
"name": "I love svelte",
"files": [
{
"name": "REPL"
},
{
"name": "oh nooo"
}
]
}
]
},
{
"name": "Favourites 1"
},
{
"name": "Favourites 2"
},
{
"name": "Favourites 3"
}
]
},
{
"name": "Knowledge Base 1"
}
]
}]
How do I flatten this array of objects such that I would get the desired output below such that, ignoring files and only caring about folders (which have files inside):我如何展平这个对象数组,以便在下面得到所需的 output,从而忽略文件,只关心文件夹(里面有文件):
All possible routes that can be taken by going through all folders, ignoring files with no other files inside them.通过遍历所有文件夹,忽略其中没有其他文件的文件,可以采取所有可能的路线。
Knowledge Base > Documents知识库 > 文档
Knowledge Base > Favourites > Brawl Stars知识库 > 收藏夹 > Brawl Stars
Knowledge Base > Favourites > Coding知识库 > 收藏夹 > 编码
Knowledge Base > Favourites > Coding > I love svelte知识库 > 收藏夹 > 编码 > 我喜欢 svelte
I figured out using a recursive function, but I dont know how to go from there.我想出了使用递归 function,但我不知道如何从那里到 go。
In your recursive function just check if object has files
property:在您的递归 function 中,只需检查 object 是否具有files
属性:
const data = [{ "name": "Knowledge Base", "files": [ { "name": "Documents", "files": [ { "name": "Quarterly Results" } ] }, { "name": "Favourites", "files": [ { "name": "Brawl Stars", "files": [ { "name": "NS dying in 5 seconds" }, { "name": "Josiah raping NS" } ] }, { "name": "Coding", "files": [ { "name": "Coding is so fun" }, { "name": "I love svelte", "files": [ { "name": "REPL" }, { "name": "oh nooo" } ] } ] }, { "name": "Favourites 1" }, { "name": "Favourites 2" }, { "name": "Favourites 3" } ] }, { "name": "Knowledge Base 1" } ] }]; function flatten(obj, parent = "") { parent += obj.name + ">"; //append current folder name let result = []; if (obj.files) { result = obj.files.filter(file => file.files) //only leave folders.map(file => parent + file.name);//add names } if (Array.isArray(obj.files)) result = result.concat(...obj.files.map(folder => flatten(folder, parent))); //recursivly call flatten for next subfolder return result; } const dataFlat = data.reduce((a,b) => (a.push(...flatten(b)), a), []); console.log(dataFlat);
const rootFiles = [{"name":"Knowledge Base","files":[{"name":"Documents","files":[{"name":"Quarterly Results"}]},{"name":"Favourites","files":[{"name":"Brawl Stars","files":[{"name":"NS dying in 5 seconds"},{"name":"Josiah raping NS"}]},{"name":"Coding","files":[{"name":"Coding is so fun"},{"name":"I love svelte","files":[{"name":"REPL"},{"name":"oh nooo"}]}]},{"name":"Favourites 1"},{"name":"Favourites 2"},{"name":"Favourites 3"}]},{"name":"Knowledge Base 1"}]}] function getDirectoryPaths(files) { return files.flatMap(({name, files: childFiles}) => childFiles? [ name, ...getDirectoryPaths(childFiles).map(childPath=>`${name}>${childPath}`) ]: []) } console.log(getDirectoryPaths(rootFiles)) // or, if you don't want the top-level directory listed: console.log(getDirectoryPaths(rootFiles).slice(1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.