-1

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.

Pawel Kam
  • 1,684
  • 3
  • 14
  • 30
  • 1
    can you share code of your approach? – ashish singh Jan 12 '23 at 18:41
  • 1
    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); } }) This is for first level.. similarly i have coded for 2,3,4 levels.. – Kapil Balwani Jan 12 '23 at 18:48
  • Does this answer your question? [Build tree array from flat array in javascript](https://stackoverflow.com/questions/18017869/build-tree-array-from-flat-array-in-javascript) – pilchard Jan 12 '23 at 19:02
  • or [Turning flat array of objects into nested tree (location data)](https://stackoverflow.com/questions/71329339/turning-flat-array-of-objects-into-nested-tree-location-data) – pilchard Jan 12 '23 at 19:03
  • Also, to mention level keys can also change - like instead of lvl1,lvl2,lvl3,lvl4 it can be anything – Kapil Balwani Jan 12 '23 at 19:15

1 Answers1

0

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392