var data = [{
"id": 1,
"uuid": "FDB585DB-21A0-47F2-A422-4F7E76037915",
"deptname": "新舟智能",
"parentdeptuuid": "-1",
"deptcode": "xzkj",
"remark": "备注",
"createuser": "admin",
"createdate": "2019-03-14 10:51:20",
"modifyuser": "admin",
"modifydate": "2019-03-14 11:43:23",
"deletestatus": 0,
"depttype": "dept"
}, {
"id": 2,
"uuid": "5bf3e5b0-1bdb-4ee8-b095-5245aefbbdc6",
"deptname": "新舟智能运营部",
"parentdeptuuid": "FDB585DB-21A0-47F2-A422-4F7E76037915",
"deptcode": "xzkjyy",
"remark": "备注",
"createuser": "152dacbd-8e69-45b1-9b32-7c8e9e5e49e7",
"createdate": "2019-03-27 10:24:50",
"modifyuser": "152dacbd-8e69-45b1-9b32-7c8e9e5e49e7",
"modifydate": "2019-03-27 10:30:27",
"deletestatus": 0,
"depttype": "dept"
}, {
"id": 3,
"uuid": "683259DF-F1D2-4050-AF8F-D9589E7A205A",
"deptname": "新舟智能研发部",
"parentdeptuuid": "FDB585DB-21A0-47F2-A422-4F7E76037915",
"deptcode": "xzkjyf",
"remark": "备注",
"createuser": "152dacbd-8e69-45b1-9b32-7c8e9e5e49e7",
"createdate": "2019-03-27 10:24:50",
"modifyuser": "152dacbd-8e69-45b1-9b32-7c8e9e5e49e7",
"modifydate": "2019-03-27 10:30:27",
"deletestatus": 0,
"depttype": "dept"
}, {
"id": 4,
"uuid": "C3911066-D7A8-44BF-A1C4-7C1C91C49D75",
"deptname": "会签系统项目组",
"parentdeptuuid": "683259DF-F1D2-4050-AF8F-D9589E7A205A",
"deptcode": "yfejs",
"remark": "备注",
"createuser": "152dacbd-8e69-45b1-9b32-7c8e9e5e49e7",
"createdate": "2019-03-27 10:24:50",
"modifyuser": "152dacbd-8e69-45b1-9b32-7c8e9e5e49e7",
"modifydate": "2019-03-27 10:30:27",
"deletestatus": 0,
"depttype": "dept"
}]
function convert(data, parentdeptuuid) {
let convertData = [];
data.forEach((item, index) => {
if (item.parentdeptuuid == parentdeptuuid) {
console.log(item.deptname)
convertData.push(item);
convertChild(data, item, item.uuid)
}
})
return convertData;
}
function convertChild(arr,parentItem,parentdeptuuid){
parentItem.children = parentItem.children?parentItem.children:[];
arr.forEach((item)=>{
if (item.parentdeptuuid == parentdeptuuid) {
parentItem.children.push(item);
convertChild(data, item, item.uuid)
}
})
return parentItem.children;
}
console.log(convert(data,-1));
转载于:https://www.cnblogs.com/mrt-yyy/p/10676549.html