I've been having a problem trying to flatten the folders in this format: for example we have this folder structure. The names should be changed as on the right side, whether as the format of each folder should be flattened. The goal is for the folders to be flattened and each of their names should be for example: If A has a subfolder B and subfolder C, C's name should be: A/B/C, B's name should be 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",
},
],
}
CodePudding user response:
Maybe something like this gets you started? Here flat
is a recursive generator that yields array paths of name
attributes -
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
Run the snippet below to verify the result in your own browser -
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; }
Here's a second option that builds a path of nodes objects, not just the name
properties. This gives the caller the option to use any node properties when constructing the output path -
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>
Run the snippet below to verify the result in your own browser -
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; }