Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

json - Javascript array of object to tree structure

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...