function Gentree (list) { var temptree = [], tree = [], items = []; for (var i in list) { & nbsp if (!temptree[list[i].did]) { var trow = { &NBS P ID: ' z ' + list[i].did, Did:list[i].did , fid:list[i].fid, text:list[i].text, ICONCLS: ' Im_role ', children: [] }; &N Bsp Temptree[list[i].did] = trow; Items.push (Trow); } if (List[i].uid > 0) { Temptree[list[i].di d][' ChildrEn '].push ({ id:list[i].uid, &NBS P text:list[i].realname, ICONCLS: ' Im_user ' &NBS P }); } } for (Var j in items) {  ; if (Temptree[items[j].fid]) { temptree[items[j].fid][' child ren '].push (Temptree[items[j].did]); } else { Tree.push (Temptree[items[j].did]); } } Temptree = null; items = n ull; return tree; } var list=[//directory {did:1,fid:0,text: ' 1111 '}, {did:2, Fid:1,text: ' 2222 '}, {did:3,fid:1,text: ' 3333 '}, {did:4,fid:1,text: ' 4444 '}, { Did:5,fid:3,text: ' 5555 '},&nbsP {did:6,fid:4,text: ' 6666 '}, {did:7,fid:5,text: ' 7777 '}, {did:8,fid:3,text: ' 8888 '}, {did:9,fid:4,text: ' 9999 '}, //directory under the people {uid:1,did:2,realname: ' r111 '}, {uid:2,did:9,realname: ' r222 '}, {uid:3,did:2,realname: ' r333 '}, {uid:4,did:6, Realname: ' r444 '}, {uid:5,did:7,realname: ' r555 '}, {uid:6,did:8,realname: ' r666 '}, {uid:7,did:9,realname: ' r777 '}, //or in the same record, the data needs to be sorted first to ensure that the directory is in front {did:10,fid:1, Text: ' 1010 ', Uid:10,realname: ' r1010 '}, {did:11,fid:10,text: ' 1111 ', uid:11,realname: ' r1111 '}, {did:12,fid:11,text: ' 1212 ', Uid:12,realname: ' r1212 '}, {did:13,fid:12,text: ' 1313 ', uid:13, Realname: ' r1313 '}];var tree=gentree (list) Console.log (tree); /*[ { "id": "Z1", "did": 1, "FID": 0, "text": "1111", "iconcls": "Im_role", "Children": [&N Bsp { "id": "Z2",   ; "did": 2, "FID": 1, "text": "2222", "Iconcls ":" Im_role ", " Children ": [ &NBSP ; { "id": 1,& nbsp "text": "r111", &N Bsp "iconcls": "Im_user" &NBS P   }, { "id": 3, &N Bsp "text": "r333", "iconcls": "Im_u Ser " } &NBSP ; ] }, { "id": "z3", "did": 3, &NB Sp "FID": 1, "text": "3333", "iconcls": "Im_role", &NB Sp "ChIldren ": [ { "id": "Z5", &NBSP ; did: 5, "FID": 3, "text": "5555", & nbsp "iconcls": "Im_role", &NB Sp "Children": [ &NBS P { &N Bsp "id": "z7", "DiD ": 7, " FID ": 5, " text ":" 7777 ", "Iconcls": "Im_role", &NB Sp "Children": [ &NBS P { &N Bsp "id": 5, &N Bsp "text": "r555", &N Bsp &nBsp "iconcls": "Im_user" &NBS P } &N Bsp ] &N Bsp } &N Bsp ] }, { [i] D ":" Z8 ", " did ": 8, "FID": 3,   "text": "8888", &N Bsp "Iconcls": "Im_role", "Children": [&nbs P { &N Bsp "id": 6, &N Bsp "text": "r666", &N Bsp "iconcls": "Im_user" &NBS P } &N Bsp ] } &NBsp ] }, { "id": "Z4", &NBSP ; "Did": 4, "FID": 1, "text": "4444", "iconcls": "Im_role", & nbsp "Children": [ { &NB Sp "id": "Z6", "did": 6, "FID": 4, "text": "6666", "iconcls": "Im_role", &NB Sp "Children": [ &NBS P { &N Bsp "id": 4, &N Bsp "text": "r444", &N Bsp ' iconcls ': ' Im_user ' &NBS P } ] and nbsp }, { "id": "z9", "did": 9, "FID": 4, Text ":" 9999 ", " iconcls ":" Im_role ", &NB Sp "Children": [ &NBS P { &N Bsp "id": 2, &N Bsp "text": "r222", &N Bsp &nBsp "iconcls": "Im_user" &NBS P }, . nbsp { "id": 7,& nbsp "text": "r777", &N Bsp ICONCLS "Im_us": "The" Er " } ] & nbsp } ] }, { "id": "z10", &NB Sp "did": 10, "FID": 1,   ; "text": "1010", "ICONCLS" : "Im_role", "Children": [ { "id": 10 , "text": "r1010", "iconcls": "Im_user" &NBS P }, {   "id": "Z11", &NBSP ; did: 11, "FID": 10,&nbs P "text": "1111", &N Bsp "iconcls": "Im_role", &NB Sp "Children": [ &NBS P { &N Bsp "id": 11, " Text ":" r1111 ",   "Iconcls": "Im_user" &NBS P }, { "id": "z12",   ; "did": 12, "FID": 11, "text": "1212", &N Bsp "iconcls": "Im_role", &nb Sp children "[ ": &nbs P { &N Bsp / "ID : 12, &NBS P "text": "r1212", and nbsp "iconcls": "Im_user" &NBS P }, & nbsp { &N Bsp "id": "z13", "D ID ": 13, &N Bsp FID: 12, "text": "1313", &N Bsp "iconcls": "Im_role", &NB Sp "Children" : [ { &N Bsp "id": 13, & nbsp "TE XT ":" r1313 ", &NBS P "iconcls": "Im_user" &NBS P } &N Bsp , ]&N Bsp , }&N Bsp ] &nbsP } &N Bsp ] } & nbsp ] } ] }]*/
JS version of infinite Classification tree data formatting (mate recursion)