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