压扁文件夹



我在尝试以这种格式展开文件夹时遇到了问题:例如,我们有这样的文件夹结构。名称应更改为右侧的名称,无论每个文件夹的格式是否应展开。目标是将文件夹展开,每个文件夹的名称应为例如:如果A有子文件夹B和子文件夹C,C的名称应该是:A/B/C,B的名称应该为A/B。

{
id: "0",
name: null,
parentId: null,
folderType: "chatMessages",
folders: [
{
id: 3195588631115178,
name: "Testfolder",
parentId: null,
folderType: "chatMessages",
folders: [
{
id: "3195588620182363",
name: "Subfolder",
parentId: "3195588631115178",
folderType: "chatMessages",
folders: [
{
id: "3206824598737435",
name: "Interesting",
parentId: "3195588620182363",
folderType: "chat",
folders: [],
items: [
{
id: "3208409930553392",
name: "Message",
folderId: "3206824598737435",
updated: "2022-05-27T07:28:40.450Z",
frontendFolderId: null,
text: "<p>This is an HTML with Image.</p>",
keywords: "test",
subject: "What kind of subject",
slashCommand: "test",
language: "en-US",
setupItemId: "3208409930553392",
},
],
},
],
items: [
{
id: "3195595211854821",
name: "Message in subfolder",
folderId: "3195588620182363",
updated: "2022-05-19T12:05:39.503Z",
frontendFolderId: null,
text: "Message in subfolder",
keywords: "test",
subject: "Message in subfolder",
slashCommand: "sub",
language: "bn-BD",
setupItemId: "3195595211854821",
},
],
},
],
items: [],
},
],
items: [
{
id: "2888102250465731",
name: "bye",
folderId: null,
updated: "2022-05-25T11:15:36.367Z",
frontendFolderId: null,
text: "Thanks for contacting us.  Please do not hesitate to contact us again if we can be of further assistance.",
keywords: "bye",
subject: null,
slashCommand: null,
language: null,
setupItemId: "2888102250465731",
},
],
}

更新:如何创建每个项目及其id、folderId和文本属性的数组?我想实现这种格式:

{
id: "3195595211854821",
folderId: "3195588620182363",
text: "Message in subfolder",
}

也许这样的事情能让你开始?这里flat是一个递归生成器,它产生name属性的数组路径-

function *flat({ name = "", folders = [], items = [] }) {
yield [name]
for (const x of [...folders, ...items])
for (const path of flat(x))
yield [name, ...path]
}
for (const path of flat(data))
console.log(path.join("/"))

/Testfolder
/Testfolder/Subfolder
/Testfolder/Subfolder/Interesting
/Testfolder/Subfolder/Interesting/Message
/Testfolder/Subfolder/Message in subfolder
/bye

运行下面的代码段,在您自己的浏览器-中验证结果

const data  = {id:"0",name:null,parentId:null,folderType:"chatMessages",folders:[{id:3195588631115178,name:"Testfolder",parentId:null,folderType:"chatMessages",folders:[{id:"3195588620182363",name:"Subfolder",parentId:"3195588631115178",folderType:"chatMessages",folders:[{id:"3206824598737435",name:"Interesting",parentId:"3195588620182363",folderType:"chat",folders:[],items:[{id:"3208409930553392",name:"Message",folderId:"3206824598737435",updated:"2022-05-27T07:28:40.450Z",frontendFolderId:null,text:"<p>This is an HTML with Image.</p>",keywords:"test",subject:"What kind of subject",slashCommand:"test",language:"en-US",setupItemId:"3208409930553392",},],},],items:[{id:"3195595211854821",name:"Message in subfolder",folderId:"3195588620182363",updated:"2022-05-19T12:05:39.503Z",frontendFolderId:null,text:"Message in subfolder",keywords:"test",subject:"Message in subfolder",slashCommand:"sub",language:"bn-BD",setupItemId:"3195595211854821",},],},],items:[],},],items:[{id:"2888102250465731",name:"bye",folderId:null,updated:"2022-05-25T11:15:36.367Z",frontendFolderId:null,text:"Thanks for contacting us.  Please do not hesitate to contact us again if we can be of further assistance.",keywords:"bye",subject:null,slashCommand:null,language:null,setupItemId:"2888102250465731",},],}
function *flat({ name = "", folders = [], items = [] }) {
yield [name]
for (const x of [...folders, ...items])
for (const path of flat(x))
yield [name, ...path]
}
for (const path of flat(data))
console.log(path.join("/"))
.as-console-wrapper { min-height: 100%; top: 0; }

这里有第二个选项,它构建节点对象的路径,而不仅仅是name属性。这为调用者提供了在构造输出路径-时使用任何节点属性的选项

function *flat(t = {}) {
yield [t]
for (const x of [...t.folders ?? [], ...t.items ?? []])
for (const path of flat(x))
yield [t, ...path]
}
for (const path of flat(data))
console.log(
path.map(node => `${node.name ?? ""}<#${node.id}>`).join("/")
)
<#0>
<#0>/Testfolder<#3195588631115178>
<#0>/Testfolder<#3195588631115178>/Subfolder<#3195588620182363>
<#0>/Testfolder<#3195588631115178>/Subfolder<#3195588620182363>/Interesting<#3206824598737435>
<#0>/Testfolder<#3195588631115178>/Subfolder<#3195588620182363>/Interesting<#3206824598737435>/Message<#3208409930553392>
<#0>/Testfolder<#3195588631115178>/Subfolder<#3195588620182363>/Message in subfolder<#3195595211854821>
<#0>/bye<#2888102250465731>

运行下面的代码段,在您自己的浏览器-中验证结果

const data  = {id:"0",name:null,parentId:null,folderType:"chatMessages",folders:[{id:3195588631115178,name:"Testfolder",parentId:null,folderType:"chatMessages",folders:[{id:"3195588620182363",name:"Subfolder",parentId:"3195588631115178",folderType:"chatMessages",folders:[{id:"3206824598737435",name:"Interesting",parentId:"3195588620182363",folderType:"chat",folders:[],items:[{id:"3208409930553392",name:"Message",folderId:"3206824598737435",updated:"2022-05-27T07:28:40.450Z",frontendFolderId:null,text:"<p>This is an HTML with Image.</p>",keywords:"test",subject:"What kind of subject",slashCommand:"test",language:"en-US",setupItemId:"3208409930553392",},],},],items:[{id:"3195595211854821",name:"Message in subfolder",folderId:"3195588620182363",updated:"2022-05-19T12:05:39.503Z",frontendFolderId:null,text:"Message in subfolder",keywords:"test",subject:"Message in subfolder",slashCommand:"sub",language:"bn-BD",setupItemId:"3195595211854821",},],},],items:[],},],items:[{id:"2888102250465731",name:"bye",folderId:null,updated:"2022-05-25T11:15:36.367Z",frontendFolderId:null,text:"Thanks for contacting us.  Please do not hesitate to contact us again if we can be of further assistance.",keywords:"bye",subject:null,slashCommand:null,language:null,setupItemId:"2888102250465731",},],}
function *flat(t = {}) {
yield [t]
for (const x of [...t.folders ?? [], ...t.items ?? []])
for (const path of flat(x))
yield [t, ...path]
}
for (const path of flat(data))
console.log(
path.map(node => `${node.name ?? ""}<#${node.id}>`).join("/")
)
.as-console-wrapper { min-height: 100%; top: 0; }

最新更新