I want to convert flat structure array into a tree structure for one my projects. Below is the input and expected output:
Input:
let input=[
{
lvl1:"Code1",
lvl2:"Type1",
lvl3:"Desc1",
lvl4:"Check1"
},
{
lvl1:"Code1",
lvl2:"Type1",
lvl3:"Desc1",
lvl4:"Check2"
},
{
lvl1:"Code2",
lvl2:"Type2",
lvl3:"Desc2",
lvl4:"Check1"
},
]
Output:
[
{
level_key:"lvl1",
level_value:"Code1",
children:[
{
level_key:"lvl2",
level_value:"Type1",
children:[
{
level_key:"lvl3",
level_value:"Desc1",
children:[
{
level_key:"lvl4",
level_value:"Check1",
children:[]
},
{
level_key:"lvl4",
level_value:"Check2",
children:[]
}
]
}
]
}
]
},
{
level_key:"lvl1",
level_value:"Code2",
children:[
{
level_key:"lvl2",
level_value:"Type2",
children:[
{
level_key:"lvl3",
level_value:"Desc2",
children:[
{
level_key:"lvl4",
level_value:"Check1",
children:[]
}
]
}
]
}
]
}
]
Here in eg. i have taken till lvl4 but any number of levels could be there like lvl5, lvl6....
I have tried a approach but i feel that is very complex and not scalable.
CodePudding user response:
You could take obbjects with level_value
as key and take the arrays as result.
const
flat = [{ lvl1:"Code1", lvl2:"Type1", lvl3:"Desc1", lvl4:"Check1" }, { lvl1:"Code1", lvl2:"Type1", lvl3:"Desc1", lvl4:"Check2" }, { lvl1:"Code2", lvl2:"Type2", lvl3:"Desc2", lvl4:"Check1" }],
tree = flat.reduce((r, o) => {
let temp = r,
i = 1,
level_key = `lvl${i}`,
level_value = o[level_key];
do {
if (!temp[level_value]) {
temp[level_value] = { _: [] };
temp._.push({ level_key, level_value, children: temp[level_value]._ });
}
temp = temp[level_value];
level_key = `lvl${ i}`;
level_value = o[level_key];
} while (level_value)
return r;
}, { _: [] })._;
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
I have tried this:
let output=[]
input.forEach(ele=>{
let flag=1;
output.forEach(outele=>{
if(ele.lvl1==outele.level_value){
flag=0;
}
})
if(flag==1){
let obj={level_key:"lvl1",level_value:ele.lvl1,children:[]};
output.push(obj);
}
})
input.forEach(ele=>{
output.forEach(outele=>{
let flag=1;
if(ele.lvl1==outele.level_value){
outele.children.forEach(outele2=>{
if(ele.lvl2==outele2.level_value){
flag=0;
}
})
if(flag==1){
let obj={level_key:"lvl2",level_value:ele.lvl2,children:[]};
outele.children.push(obj);
}
}
})
})
input.forEach(ele=>{
output.forEach(outele=>{
outele.children.forEach(outele2=>{
if(ele.lvl1==outele.level_value && ele.lvl2==outele2.level_value){
let flag=1;
outele2.children.forEach(outele3=>{
if(ele.lvl3==outele3.level_value){
flag=0;
}
})
if(flag==1){
let obj={level_key:"lvl3",level_value:ele.lvl3,children:[]};
outele2.children.push(obj);
}
}
})
})
})
input.forEach(ele=>{
output.forEach(outele=>{
outele.children.forEach(outele2=>{
outele2.children.forEach(outele3=>{
if(ele.lvl1==outele.level_value && ele.lvl2==outele2.level_value && ele.lvl3==outele3.level_value){
let flag=1;
outele3.children.forEach(outele4=>{
if(ele.lvl4==outele4.level_value){
flag=0;
}
})
if(flag==1){
let obj={level_key:"lvl4",level_value:ele.lvl4,children:[]};
outele3.children.push(obj);
}
}
})
})
})
})