1.方法
buildDeptTree(deptData) { //构建树状部门
// 创建一个 map 来存储 deptId 和对应的部门对象
const deptMap = new Map();
// 初始化每个部门对象的 children 属性为空数组
deptData.forEach(dept => {
dept.children = [];
deptMap.set(dept.deptId, dept);
});
// 构建树形结构
let rootDepts = [];
deptData.forEach(dept => {
if (dept.parentId === 0) {
rootDepts.push(dept);
} else {
const parentDept = deptMap.get(dept.parentId);
if (parentDept) {
parentDept.children.push(dept);
}
}
});
// 移除所有 children 为空的数组
function removeEmptyChildren(depts) {
depts.forEach(dept => {
if (dept.children.length === 0) {
delete dept.children;
} else {
removeEmptyChildren(dept.children);
}
});
}
removeEmptyChildren(rootDepts);
return rootDepts;
},
2.应用
this.buildDeptTree(res.data);
3.数据:
[
{
"createBy": "admin",
"createTime": "2023-09-06 08:20:03",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 100,
"parentId": 0,
"ancestors": "0",
"deptName": "普陀",
"orderNum": 0,
"leader": "admin",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:43:21",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 262,
"parentId": 100,
"ancestors": "0,100",
"deptName": "投促办",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-18 11:20:17",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 312,
"parentId": 100,
"ancestors": "0,100",
"deptName": "委办",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2023-09-06 08:20:03",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 101,
"parentId": 100,
"ancestors": "0,100",
"deptName": "上海联通分公司",
"orderNum": 1,
"leader": "若依",
"phone": "15888888888",
"email": "ry@qq.com",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2024-12-20 14:41:07",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 232,
"parentId": 100,
"ancestors": "0,100",
"deptName": "诉求",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:51:51",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 266,
"parentId": 100,
"ancestors": "0,100",
"deptName": "街道",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2023-10-17 14:25:10",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 210,
"parentId": 100,
"ancestors": "0,100",
"deptName": "XX联通分公司",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2023-09-06 08:20:03",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 103,
"parentId": 101,
"ancestors": "0,100,101",
"deptName": "研发部门",
"orderNum": 1,
"leader": "若依",
"phone": "15888888888",
"email": "ry@qq.com",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2023-09-06 08:20:03",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 105,
"parentId": 101,
"ancestors": "0,100,101",
"deptName": "测试部门",
"orderNum": 2,
"leader": "若依",
"phone": "15888888888",
"email": "ry@qq.com",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2024-12-20 14:41:40",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 243,
"parentId": 232,
"ancestors": "0,100,232",
"deptName": "诉求协办部门",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2024-12-20 14:41:29",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 242,
"parentId": 232,
"ancestors": "0,100,232",
"deptName": "诉求会办部门",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2024-12-20 14:41:20",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 233,
"parentId": 232,
"ancestors": "0,100,232",
"deptName": "诉求主办部门",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 16:34:55",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 303,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "桃浦地区投促中心",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 16:35:09",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 304,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "真如地区投促中心",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 16:35:18",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 305,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "长风地区投促中心",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 16:35:27",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 306,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "长征地区投促中心",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:47:25",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 263,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "项目推进科",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:47:36",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 272,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "企业服务科",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:47:47",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 273,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "地区指导科",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:50:39",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 264,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "经济运行科",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:50:22",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 282,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "办公室主任",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:51:30",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 265,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "综合科",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:51:39",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 274,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "产业发展科",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 16:34:42",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 302,
"parentId": 262,
"ancestors": "0,100,262",
"deptName": "长寿地区投促中心",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:52:56",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 285,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "石泉路街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:52:30",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 292,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "长寿路街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:52:39",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 283,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "曹杨路街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:52:47",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 284,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "宜川路街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:53:03",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 286,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "甘泉路街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:53:11",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 287,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "真如镇街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:53:17",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 288,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "万里街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:53:25",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 289,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "长风街道",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:53:34",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 290,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "长征镇",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-02 10:53:43",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 291,
"parentId": 266,
"ancestors": "0,100,266",
"deptName": "桃浦镇",
"orderNum": 2,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
},
{
"createBy": "admin",
"createTime": "2025-01-18 11:20:28",
"updateBy": null,
"updateTime": null,
"remark": null,
"deptId": 313,
"parentId": 312,
"ancestors": "0,100,312",
"deptName": "财政局",
"orderNum": 1,
"leader": "",
"phone": "",
"email": "",
"status": "0",
"delFlag": "0",
"parentName": null,
"children": []
}
]