js生成动态树状结构及排序

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> </body> </html> <script> var obj = [{"tid":"8","vid":"2","name":"Finance and insurance","description":"","format":"plain_text","weight":"11","uuid":"046407e3-e150-48a1-ae9c-5b98278719ec","parent":"0","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/8"},{"tid":"29","vid":"2","name":"Astronomy","description":"","format":"plain_text","weight":"6","uuid":"a5f418f1-c7e6-42ca-ae98-cfcb59ec6486","parent":"11","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/29"},{"tid":"16","vid":"2","name":"Chemistry","description":"","format":"plain_text","weight":"5","uuid":"4c471efe-162f-4902-acdd-b579b39d129c","parent":"11","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/16"},{"tid":"40","vid":"2","name":"Small business","description":"","format":"plain_text","weight":"4","uuid":"e68253b0-051a-48d5-9baa-39363015df5f","parent":"26","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/40"},{"tid":"15","vid":"2","name":"Biology","description":"","format":"plain_text","weight":"4","uuid":"4be4593f-3f5f-4369-8fdd-8669a45cdbf8","parent":"11","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/15"},{"tid":"21","vid":"2","name":"Sculpting","description":"","format":"plain_text","weight":"3","uuid":"7a81371d-aac5-4d94-a640-d680ee9d15cd","parent":"17","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/21"},{"tid":"34","vid":"2","name":"Other","description":"","format":"plain_text","weight":"3","uuid":"caf667aa-b20f-4240-bc50-0c326729e5fb","parent":"19","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/34"},{"tid":"25","vid":"2","name":"Life insurance","description":"","format":"plain_text","weight":"3","uuid":"874872ef-6417-4985-9710-a906701479d9","parent":"20","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/25"},{"tid":"45","vid":"2","name":"Jurisprudence","description":"","format":"plain_text","weight":"3","uuid":"f3562505-b759-4fb7-804b-28c35fd86d33","parent":"36","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/45"},{"tid":"19","vid":"2","name":"Education","description":"","format":"plain_text","weight":"3","uuid":"74607970-cfce-4f32-b8fb-1faf64b1ea6e","parent":"0","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/19"},{"tid":"14","vid":"2","name":"Corporate","description":"","format":"plain_text","weight":"3","uuid":"40aaf7f0-a05e-47c9-b1cb-f5258059008e","parent":"26","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/14"},{"tid":"30","vid":"2","name":"Calculus","description":"","format":"plain_text","weight":"3","uuid":"b032dae8-6ee4-4509-b57c-28edde6e0306","parent":"11","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/30"},{"tid":"35","vid":"2","name":"Toefl","description":"","format":"plain_text","weight":"2","uuid":"cc1879ba-d941-4ab4-8c41-a1d85929c7f8","parent":"34","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/35"},{"tid":"36","vid":"2","name":"Social Studies","description":"","format":"plain_text","weight":"2","uuid":"dba614a4-fb14-4e64-be0c-89c4b25f56c5","parent":"19","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/36"},{"tid":"32","vid":"2","name":"Retirement","description":"","format":"plain_text","weight":"2","uuid":"b631dd0d-af5f-4543-a087-78b0c948a46a","parent":"26","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/32"},{"tid":"43","vid":"2","name":"Property insurance","description":"","format":"plain_text","weight":"2","uuid":"ed46f1d4-883b-4b9a-a832-0fadee8f1773","parent":"20","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/43"},{"tid":"38","vid":"2","name":"Physics","description":"","format":"plain_text","weight":"2","uuid":"e13887ef-985c-45bd-ac7a-111d0480a174","parent":"11","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/38"},{"tid":"18","vid":"2","name":"Other","description":"","format":"plain_text","weight":"2","uuid":"6c6300fc-03c2-4aee-bdd7-0854f39a2128","parent":"8","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/18"},{"tid":"42","vid":"2","name":"Guitar","description":"","format":"plain_text","weight":"2","uuid":"e9519df6-a97f-4722-ac06-67b41047b10b","parent":"17","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/42"},{"tid":"33","vid":"2","name":"Geography","description":"","format":"plain_text","weight":"2","uuid":"b912a594-0721-4bda-bdb2-e8e165800002","parent":"36","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/33"},{"tid":"24","vid":"2","name":"Analytics","description":"","format":"plain_text","weight":"2","uuid":"85e93dc3-d1f8-4c08-b323-97dcc62b7c29","parent":"18","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/24"},{"tid":"37","vid":"2","name":"Tutoring","description":"","format":"plain_text","weight":"1","uuid":"de9060bb-3b77-475b-b66c-abb943bf5d50","parent":"11","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/37"},{"tid":"10","vid":"2","name":"Tax return","description":"","format":"plain_text","weight":"1","uuid":"2ead4d97-a9a1-4c6c-8be6-d1ab4596a89b","parent":"26","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/10"},{"tid":"39","vid":"2","name":"Stock markets","description":"","format":"plain_text","weight":"1","uuid":"e201de51-dbbe-4626-accf-7b4f8715c30b","parent":"18","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/39"},{"tid":"31","vid":"2","name":"Piano","description":"","format":"plain_text","weight":"1","uuid":"b592aa46-1c66-4b02-8870-f03eadd6b0c8","parent":"17","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/31"},{"tid":"13","vid":"2","name":"Javascript","description":"","format":"plain_text","weight":"1","uuid":"3a69b757-b8fd-417b-bdd6-9856e0e3d745","parent":"34","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/13"},{"tid":"20","vid":"2","name":"Insurance","description":"","format":"plain_text","weight":"1","uuid":"7740c688-afef-4faf-8da8-27c28c63063e","parent":"8","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/20"},{"tid":"28","vid":"2","name":"Economy","description":"","format":"plain_text","weight":"1","uuid":"9804d599-9ee1-42d9-86d1-78ed56b10d01","parent":"36","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/28"},{"tid":"12","vid":"2","name":"Auto insurance","description":"","format":"plain_text","weight":"1","uuid":"39347db7-11f8-41e1-a15b-33f4d765dcda","parent":"20","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/12"},{"tid":"17","vid":"2","name":"Art","description":"","format":"plain_text","weight":"1","uuid":"5cf10d30-70a6-4ab4-b600-1d4088408166","parent":"19","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/17"},{"tid":"11","vid":"2","name":"Science","description":"","format":"plain_text","weight":"0","uuid":"377f5479-40c2-48d9-865b-845639e05fea","parent":"19","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/11"},{"tid":"46","vid":"2","name":"Programming","description":"","format":"plain_text","weight":"0","uuid":"f75007da-7cfc-4588-91b2-7b582779c463","parent":"34","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/46"},{"tid":"41","vid":"2","name":"Math","description":"","format":"plain_text","weight":"0","uuid":"e91dfbdf-7b0c-469d-ae81-2a91c68a1f1f","parent":"11","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/41"},{"tid":"44","vid":"2","name":"Investments","description":"","format":"plain_text","weight":"0","uuid":"ef4af7ba-79d7-41cd-a964-6950f9971ea0","parent":"26","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/44"},{"tid":"27","vid":"2","name":"History","description":"","format":"plain_text","weight":"0","uuid":"91d54e55-3b96-4aea-8062-e7e49b2040e7","parent":"36","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/27"},{"tid":"22","vid":"2","name":"Health insurance","description":"","format":"plain_text","weight":"0","uuid":"7b1f8a7d-1347-419d-a32b-1d3d58422cc6","parent":"20","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/22"},{"tid":"26","vid":"2","name":"Finance","description":"","format":"plain_text","weight":"0","uuid":"913f10d8-4a38-4e8e-8f7a-d5c65f5fb523","parent":"8","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/26"},{"tid":"23","vid":"2","name":"Drawing","description":"","format":"plain_text","weight":"0","uuid":"8249d420-1c31-4d9e-a93a-a4798fefa88e","parent":"17","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/23"},{"tid":"9","vid":"2","name":"Business planning","description":"","format":"plain_text","weight":"0","uuid":"2dbf4b77-0acb-4a9c-8334-6ce7bde85360","parent":"18","uri":"https://api-zhilongy.poy.cn/api/taxonomy_term/9"}] function toTree(data) { data.forEach(function(item) { delete item.children; }); // console.log(data) var map = {}; data.forEach(function(item) { map[item.tid] = item; }); // console.log(map) var val = []; data.forEach(function(item) { var parent = map[item.parent]; if (parent) { (parent.children || (parent.children = [])).push(item); } else { val.push(item); } }); return val; } function compare(obj1, obj2) { var val1 = Number(obj1.weight); var val2 = Number(obj2.weight); if (val1 < val2) { return -1; } else if (val1 > val2) { return 1; } else { return 0; } } let datalist = toTree(obj) datalist.sort(compare).forEach(a=>{ a.children.sort(compare).forEach(b =>{ b.children.sort(compare) }) }) console.log(datalist) </script> //需要根据自己的需求改
上一篇:python中threading开启关闭线程


下一篇:MySQL-03-键及CRUD