const tree = [
{
"ScaleType": "37110870-a254-4f32-9bd4-56e47ebbc468",
"ScaleTypeCode": "0,1,2",
"Category": "6BD3F45A-D9A7-40A2-A5A6-6C8963746002",
"CategoryName": "部门",
"id": "169dc24d-9243-43f5-8299-9173f1cab1ca",
"text": "输电管理中心",
"parentId": "6b62df84-9386-4415-8965-5800d95a50a8",
"data": null
},
{
"ScaleType": "38c1093c-3f50-4573-b786-26f1c1d9da2e",
"ScaleTypeCode": "2",
"Category": "",
"CategoryName": null,
"id": "d5f456de-5e37-4337-a6bf-22e8172cf9a7",
"text": "变电运行中心",
"parentId": "6b62df84-9386-4415-8965-5800d95a50a8",
"data": null
}
]
// const tree = [
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": "f7d1ebd9-7706-4592-a5af-6b78b2ab7e4a",
// "CategoryName": "市公司班组",
// "id": "09e073d1-1cd6-4fe5-8d10-d640f134b901",
// "text": "输电运检室",
// "parentId": "fd1aedf3-8e48-4e16-9a81-dfc8db62f90a",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": "3895a3a8-0008-4e09-b9c1-610230471898",
// "CategoryName": "县公司",
// "id": "0b3d6f0e-507c-4455-b9bf-4dd53f7b98d1",
// "text": "输电运检班组",
// "parentId": "09e073d1-1cd6-4fe5-8d10-d640f134b901",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": null,
// "CategoryName": null,
// "id": "2ab387e0-3942-4d24-9b8d-e37e5a4c9ff7",
// "text": "新余供电公司输电管理中心",
// "parentId": "0",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": null,
// "CategoryName": null,
// "id": "2ce30eee-f940-456c-b617-68a4b380ef48",
// "text": "运维一班",
// "parentId": "0b3d6f0e-507c-4455-b9bf-4dd53f7b98d1",
// "data": null
// },
// {
// "ScaleType": "11a97cee-afb2-479c-bbf9-f06263a330b8",
// "ScaleTypeCode": "2",
// "Category": null,
// "CategoryName": null,
// "id": "6643fe0f-0f9a-4e1c-ac8f-74070998c6b5",
// "text": "检修二班",
// "parentId": "0b3d6f0e-507c-4455-b9bf-4dd53f7b98d1",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": null,
// "CategoryName": null,
// "id": "6e08c966-720f-48b3-867b-13a9a166836b",
// "text": "机场巡视班组",
// "parentId": "0",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": null,
// "CategoryName": null,
// "id": "72cc8821-be36-4323-8916-eb95d0f82649",
// "text": "辅助班",
// "parentId": "0b3d6f0e-507c-4455-b9bf-4dd53f7b98d1",
// "data": null
// },
// {
// "ScaleType": "11a97cee-afb2-479c-bbf9-f06263a330b8",
// "ScaleTypeCode": "2",
// "Category": "",
// "CategoryName": null,
// "id": "84080da6-b052-42c2-8c2e-2156dacedb68",
// "text": "变电运检中心",
// "parentId": "0",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": "3895a3a8-0008-4e09-b9c1-610230471898",
// "CategoryName": "县公司",
// "id": "862b6501-2748-41a4-86ad-89de9183ee65",
// "text": "分宜公司",
// "parentId": "09e073d1-1cd6-4fe5-8d10-d640f134b901",
// "data": null
// },
// {
// "ScaleType": "11a97cee-afb2-479c-bbf9-f06263a330b8",
// "ScaleTypeCode": "2",
// "Category": null,
// "CategoryName": null,
// "id": "87e82206-8d9e-4667-9c6d-727efd664fd1",
// "text": "检修一班",
// "parentId": "0b3d6f0e-507c-4455-b9bf-4dd53f7b98d1",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": "3895a3a8-0008-4e09-b9c1-610230471898",
// "CategoryName": "县公司",
// "id": "a0320904-6aa5-414a-a7c5-2a55c5d24901",
// "text": "渝水公司",
// "parentId": "09e073d1-1cd6-4fe5-8d10-d640f134b901",
// "data": null
// },
// {
// "ScaleType": "11a97cee-afb2-479c-bbf9-f06263a330b8",
// "ScaleTypeCode": "2",
// "Category": null,
// "CategoryName": null,
// "id": "ab31725d-80a4-4448-a343-606497f54488",
// "text": "外协",
// "parentId": "09e073d1-1cd6-4fe5-8d10-d640f134b901",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": "",
// "CategoryName": null,
// "id": "d2e3c4a7-34fb-4d1b-b6ce-84268c7ce2bf",
// "text": "属地",
// "parentId": "a0320904-6aa5-414a-a7c5-2a55c5d24901",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": null,
// "CategoryName": null,
// "id": "dc184794-3d17-479c-b605-a72dbf4e0528",
// "text": "带电作业班",
// "parentId": "0b3d6f0e-507c-4455-b9bf-4dd53f7b98d1",
// "data": null
// },
// {
// "ScaleType": "6d33d2dc-8d33-4848-8af8-597e25824d38",
// "ScaleTypeCode": "0,1,2",
// "Category": null,
// "CategoryName": null,
// "id": "fd1aedf3-8e48-4e16-9a81-dfc8db62f90a",
// "text": "部门领导",
// "parentId": "2ab387e0-3942-4d24-9b8d-e37e5a4c9ff7",
// "data": null
// }
// ]
const idArr = tree.map(item => item.id)
// console.log('所有id集合', idArr)
tree.map(item => {
if (!!item.parentId) {
if (idArr.indexOf(item.parentId) < 0) {
item.parentId = '0'
}
}
return item
})
// console.log(tree)
function CombinationTree(array, ParentId, IndexId, childrenKey = "children") {
let arrAll = JSON.parse(JSON.stringify(array)),
arrParent = [];
if (!Array.isArray(arrAll) || arrAll.length == 0) {
return arrParent;
}
let length = arrAll.length;
for (let i = 0; i < length; i++) {
if (
!arrAll[i][ParentId] ||
arrAll[i][ParentId] == "null" ||
arrAll[i][ParentId] == "0"
) {
arrParent.push(arrAll[i]);
}
}
if (arrParent.length == 0) {
let map = {};
for (var i = 0; i < length; i++) {
let parentIdValue = arrAll[i][ParentId];
let parent = map[parentIdValue];
if (!parent) {
let parents = arrAll.filter((item) => {
return item[IndexId] == parentIdValue;
});
if (parents.length == 0) { //如果没有找到父节点,他就是父节点
arrParent.push(arrAll[i]);
map[parentIdValue] = parentIdValue;
}
}
}
}
let QueryMatch = (arrParent, arrAll) => {
let length = arrParent.length;
for (let i = 0; i < length; i++) {
let thisChild = arrAll.filter((item) => {
return arrParent[i][IndexId] == item[ParentId];
});
if (thisChild.length > 0) {
arrParent[i][childrenKey] = thisChild;
QueryMatch(thisChild, arrAll);
}
}
return arrParent;
};
return QueryMatch(arrParent, arrAll);
}
const res = CombinationTree(tree, "parentId", "id")
console.log('最后结果', res)
console