def build_tree(menu_data): # add children for index, i in enumerate(menu_data): for j in menu_data: if i["id"]==j["parentId"]: if "children" not in menu_data[index].keys(): menu_data[index]["children"] = [] menu_data[index]["children"].append(j) res = [i for i in menu_data if i["parentId"]=="0"] return res
# 测试数据 data = [ { "blank": "false", "cache": "false", "component": "Layout", "icon": "eicon-setting", "id": "5d2d6d6810004bc421d13a55", "name": "sys", "parentId": "0", "path": "/admin", "permissions": "", "query": "", "sort": "1", "title": "系统设置", "type": "0", "url": "" }, { "blank": "false", "cache": "false", "component": "Layout", "icon": "eicon-setting", "id": "5d2d6d684bc421d131000a55", "name": "sys", "parentId": "5d2d6d6810004bc421d13a55", "path": "/admin", "permissions": "", "query": "", "sort": "1", "title": "系统设置", "type": "0", "url": "" }, { "blank": "false", "cache": "false", "component": "Layout", "icon": "eicon-setting", "id": "5d2d6d6814bc421d10003a55", "name": "sys", "parentId": "0", "path": "/admin", "permissions": "", "query": "", "sort": "1", "title": "系统设置", "type": "0", "url": "" }, { "blank": "false", "cache": "true", "component": "admin/menu", "icon": "eicon-menu", "id": "5d2e832314bc4247ac006727", "name": "sys-menu", "parentId": "5d2d6d6814bc421d10003a55", "path": "/menu", "permissions": "sys_menu_list", "query": "", "sort": "1", "title": "菜单管理", "type": "0", "url": "" }, { "blank": "false", "cache": "true", "component": "admin/menu", "icon": "eicon-menu", "id": "5d2e83ac00672314bc424727", "name": "sys-menu", "parentId": "5d2e832314bc4247ac006727", "path": "/menu", "permissions": "sys_menu_list", "query": "", "sort": "1", "title": "菜单管理", "type": "0", "url": "" }, ]
# 测试 b = build_tree(data) print(b)
运行结果:
[ { "blank": "false", "cache": "false", "component": "Layout", "icon": "eicon-setting", "id": "5d2d6d6810004bc421d13a55", "name": "sys", "parentId": "0", "path": "/admin", "permissions": "", "query": "", "sort": "1", "title": "系统设置", "type": "0", "url": "", "children": [ { "blank": "false", "cache": "false", "component": "Layout", "icon": "eicon-setting", "id": "5d2d6d684bc421d131000a55", "name": "sys", "parentId": "5d2d6d6810004bc421d13a55", "path": "/admin", "permissions": "", "query": "", "sort": "1", "title": "系统设置", "type": "0", "url": "" } ] }, { "blank": "false", "cache": "false", "component": "Layout", "icon": "eicon-setting", "id": "5d2d6d6814bc421d10003a55", "name": "sys", "parentId": "0", "path": "/admin", "permissions": "", "query": "", "sort": "1", "title": "系统设置", "type": "0", "url": "", "children": [ { "blank": "false", "cache": "true", "component": "admin/menu", "icon": "eicon-menu", "id": "5d2e832314bc4247ac006727", "name": "sys-menu", "parentId": "5d2d6d6814bc421d10003a55", "path": "/menu", "permissions": "sys_menu_list", "query": "", "sort": "1", "title": "菜单管理", "type": "0", "url": "", "children": [ { "blank": "false", "cache": "true", "component": "admin/menu", "icon": "eicon-menu", "id": "5d2e83ac00672314bc424727", "name": "sys-menu", "parentId": "5d2e832314bc4247ac006727", "path": "/menu", "permissions": "sys_menu_list", "query": "", "sort": "1", "title": "菜单管理", "type": "0", "url": "" } ] } ] } ]