-3

I have an array of objects like this..

var obj_1 = {id:1, text:"Title 1", checked: false, unitId:0, line: 0};
var obj_2 = {id:2, text:"Title 1.1", checked: false, unitId:1, line: 0};
var obj_3 = {id:3, text:"Title 1.2", checked: false, unitId:1, line: 1};
var obj_4 = {id:4, text:"Title 1.1.1", checked: false, unitId:0, line: 1};
var obj_5 = {id:5, text:"Title 2", checked: false, unitId:0, line: 1};

var obj_list = [obj_1,obj_2,obj_3,obj_4,obj_5];

I want to format the array into tree structure like this.

text: 'Main Root',
checked: false,
children: {
    {
        checked : false,
        text : 'Unit 1'
        children : {
            {
                checked : false,
                text : 'Line A',
                children : {
                    {id:1, text:"Title 1", unitId:0, line: 0}
                }
            },{
                checked: false,
                text: 'Line B',
                children : {
                    {id:4, text:"Title 1.1.1", unitId:0, line: 1},
                    {id:5, text:"Title 2", unitId:0, line: 1}
                }
            }
        },
    },{
        checked : false,
        text : 'Unit 2'
        children : {
            {
                checked : false,
                text : 'Line A',
                children : {
                    {id:2, text:"Title 1.1", unitId:1, line: 0}
                }
            },{
                checked: false,
                text: 'Line B',
                children : {
                    {id:2, text:"Title 1.2", unitId:1, line: 0}
                }
            }
        },

    }
}

This is my desired output, Top root called main root is static, his children is dynamic. I have tried since a long time, please help me to figure out. Thanks in advance.

1
  • Shouldn't children be an array not an object? Looks like an invalid structure to me Commented Oct 15, 2019 at 9:50

1 Answer 1

1

something along the lines

var obj_1 = {id:1, text:"Title 1", checked: false, unitId:0, line: 0};
var obj_2 = {id:2, text:"Title 1.1", checked: false, unitId:1, line: 0};
var obj_3 = {id:3, text:"Title 1.2", checked: false, unitId:1, line: 1};
var obj_4 = {id:4, text:"Title 1.1.1", checked: false, unitId:0, line: 1};
var obj_5 = {id:5, text:"Title 2", checked: false, unitId:0, line: 1};

var obj_list = [obj_1,obj_2,obj_3,obj_4,obj_5];
let result = obj_list.reduce((acc,o)=>{
    let unitId = o.unitId;
    let cur = acc[unitId];
    if(!cur){
        cur = {checked:false, text: `Unit ${unitId + 1}`, children:[]};
        acc[unitId] = cur;
    }
    cur.children.push(o);
    return acc;
},{});
result = Object.values(result).map(v=>{
    let dic = v.children.reduce((acc, o)=>{
        let lineId = o.line;
        let cur = acc[lineId];
        if(!cur){
            cur = {checked:false, text: `Line ${String.fromCharCode(lineId+65)}`, children:[]};
            acc[lineId] = cur;
        }
        cur.children.push(o);
        return acc;
    },{})
    v.children = Object.values(dic);
    return v;
})

result = {text:'Main Root', checked:false, children:result}
console.log(JSON.stringify(result,null,2))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.