直接新建一个html放进去即可验证。
一、因为后端返回的多维数组对象中一些key可能与UI框架封装的不对应,所以可能需要递归逐个去修改key值
<script>
var data = [
{
"id": 103,
"guid": "025f4798-27f5-4dff-a881-0e06d3e0b73c",
"name": "营销二组",
"parent_id": 0,
"parent_name": "",
"sort": 0,
"created_date": "2021-08-18",
"created_by": 4826,
"created_by_name": "ERP用户15496461616_653",
"modified_date": "2021-08-18",
"modified_by": 4826,
"modified_by_name": "ERP用户15496461616_653",
"status": 200,
"status_name": "正常",
"remark": "c",
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
},
{
"id": 101,
"guid": "34c49fa6-4e7d-452d-9250-254db8e7f602",
"name": "营销",
"parent_id": 0,
"parent_name": "",
"sort": 0,
"created_date": "2021-08-18",
"created_by": 4826,
"created_by_name": "ERP用户15496461616_653",
"modified_date": "2021-08-18",
"modified_by": 4826,
"modified_by_name": "ERP用户15496461616_653",
"status": 200,
"status_name": "正常",
"remark": "A",
"merchant_name": "",
"merchant_id": "83373D0631",
"children": [
{
"id": 107,
"guid": "fc640e15-732f-4cad-bcc4-6549738a1d79",
"name": "营销五组",
"parent_id": 101,
"parent_name": "营销",
"sort": 1,
"created_date": "2021-08-18",
"created_by": 4826,
"created_by_name": "ERP用户15496461616_653",
"modified_date": "2021-08-18",
"modified_by": 4826,
"modified_by_name": "ERP用户15496461616_653",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
},
{
"id": 106,
"guid": "319b0657-cb3f-4b7a-8daf-8a6779cbc53e",
"name": "营销四组",
"parent_id": 101,
"parent_name": "营销",
"sort": 1,
"created_date": "2021-08-18",
"created_by": 4826,
"created_by_name": "ERP用户15496461616_653",
"modified_date": "2021-08-18",
"modified_by": 4826,
"modified_by_name": "ERP用户15496461616_653",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
},
{
"id": 105,
"guid": "c2a51a27-26fa-4daa-a4bd-c286be57c68a",
"name": "营销三组",
"parent_id": 101,
"parent_name": "营销",
"sort": 1,
"created_date": "2021-08-18",
"created_by": 4826,
"created_by_name": "ERP用户15496461616_653",
"modified_date": "2021-08-18",
"modified_by": 4826,
"modified_by_name": "ERP用户15496461616_653",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
},
{
"id": 104,
"guid": "8e8d1d37-0636-41b8-b770-7992efef65ad",
"name": "营销二组",
"parent_id": 101,
"parent_name": "营销",
"sort": 1,
"created_date": "2021-08-18",
"created_by": 4826,
"created_by_name": "ERP用户15496461616_653",
"modified_date": "2021-08-18",
"modified_by": 4826,
"modified_by_name": "ERP用户15496461616_653",
"status": 200,
"status_name": "正常",
"remark": "V",
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
},
{
"id": 102,
"guid": "f2492898-e74b-4579-852a-fe1c09993861",
"name": "营销一组",
"parent_id": 101,
"parent_name": "营销",
"sort": 1,
"created_date": "2021-08-18",
"created_by": 4826,
"created_by_name": "ERP用户15496461616_653",
"modified_date": "2021-08-18",
"modified_by": 4826,
"modified_by_name": "ERP用户15496461616_653",
"status": 200,
"status_name": "正常",
"remark": "B",
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
}
]
},
{
"id": 32,
"guid": null,
"name": "行政部",
"parent_id": 0,
"parent_name": "",
"sort": 0,
"created_date": "2021-08-09",
"created_by": 1644,
"created_by_name": "詹",
"modified_date": "2021-08-09",
"modified_by": 1644,
"modified_by_name": "詹",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": [
{
"id": 61,
"guid": null,
"name": "行政部一组1",
"parent_id": 32,
"parent_name": "行政部",
"sort": 1,
"created_date": "2021-08-09",
"created_by": 4707,
"created_by_name": "ERP用户15649804733_562",
"modified_date": "2021-08-16",
"modified_by": 4707,
"modified_by_name": "ERP用户15649804733_562",
"status": 300,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
}
]
},
{
"id": 31,
"guid": "43c39ab2-0e13-4c49-b0a1-36a87fcddc3c",
"name": "研发部",
"parent_id": 0,
"parent_name": "",
"sort": 0,
"created_date": "2021-08-09",
"created_by": 1644,
"created_by_name": "詹",
"modified_date": "2021-08-09",
"modified_by": 1644,
"modified_by_name": "詹",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": [
{
"id": 71,
"guid": "52708c51-2278-4f1a-be06-1bfe8907bc18",
"name": "C#组",
"parent_id": 31,
"parent_name": "研发部",
"sort": 1,
"created_date": "2021-08-16",
"created_by": 1687,
"created_by_name": "ERP用户_422",
"modified_date": "2021-08-16",
"modified_by": 1687,
"modified_by_name": "ERP用户_422",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": [
{
"id": 91,
"guid": "88c627af-23be-4cac-a551-e2b04e67bfa0",
"name": "C#一组",
"parent_id": 71,
"parent_name": "C#组",
"sort": 2,
"created_date": "2021-08-16",
"created_by": 1687,
"created_by_name": "ERP用户_422",
"modified_date": "2021-08-16",
"modified_by": 1687,
"modified_by_name": "ERP用户_422",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": [
{
"id": 92,
"guid": "6fd13ddc-cb5b-40fd-a377-a66009688200",
"name": "C#一组一",
"parent_id": 91,
"parent_name": "C#一组",
"sort": 3,
"created_date": "2021-08-16",
"created_by": 1687,
"created_by_name": "ERP用户_422",
"modified_date": "2021-08-16",
"modified_by": 1687,
"modified_by_name": "ERP用户_422",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": [
{
"id": 93,
"guid": null,
"name": "C#一组55",
"parent_id": 92,
"parent_name": "C#一组一",
"sort": 4,
"created_date": "2021-08-16",
"created_by": 1687,
"created_by_name": "ERP用户_422",
"modified_date": "2021-08-16",
"modified_by": 1687,
"modified_by_name": "ERP用户_422",
"status": 200,
"status_name": "",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631"
}
]
}
]
}
]
},
{
"id": 63,
"guid": "de47ac32-d9a4-4ddc-9987-9d3afac1c80d",
"name": "前端小组",
"parent_id": 31,
"parent_name": "研发部",
"sort": 1,
"created_date": "2021-08-09",
"created_by": 4707,
"created_by_name": "ERP用户15649804733_562",
"modified_date": "2021-08-09",
"modified_by": 4707,
"modified_by_name": "ERP用户15649804733_562",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": [
{
"id": 81,
"guid": null,
"name": "js开发",
"parent_id": 63,
"parent_name": "前端小组",
"sort": 2,
"created_date": "2021-08-16",
"created_by": 4707,
"created_by_name": "ERP用户15649804733_562",
"modified_date": "2021-08-16",
"modified_by": 4707,
"modified_by_name": "ERP用户15649804733_562",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
}
]
},
{
"id": 62,
"guid": "a639dbf8-3ff2-4b3c-9449-27a2559d89b1",
"name": "Java小组",
"parent_id": 31,
"parent_name": "研发部",
"sort": 1,
"created_date": "2021-08-09",
"created_by": 4707,
"created_by_name": "ERP用户15649804733_562",
"modified_date": "2021-08-09",
"modified_by": 4707,
"modified_by_name": "ERP用户15649804733_562",
"status": 200,
"status_name": "正常",
"remark": null,
"merchant_name": "",
"merchant_id": "83373D0631",
"children": []
}
]
}
]
getTree = (indexP, arrP) => {
return new Promise((res, rej) => {
getListtree(indexP, arrP)
function getListtree(index, arr) {
for (let i = 0; i < arr.length; i++) {
arr[i].title = arr[i].name;
arr[i].key = arr[i].id;
arr[i].children = arr[i].children;
let filters = ['title', 'key', 'parent_id', 'children'];
for (let j in arr[i]) {
if (!(filters.includes(j))) {
delete arr[i][j]
}
}
if (arr[i].children) {
getListtree(0, arr[i].children)
}
}
}
res(arrP)
})
};
this.getTree(0, data).then(res => {
console.log(res)
})
</script>
二、本来数组对象应该是多维的,但是后端打散了,不管子集,直接一股脑地返回了一级,这个时候就要根据父级id去判断一个数组对象归于哪一个父级id对象的子集里。
<script>
var data = [
{
id: 4501,
name: "第四层",
parent_id: 4553,
sort: 0,
},
{
id: 4532,
name: "张三",
parent_id: 4512,
sort: 0,
},
{
id: 4542,
name: "掘金枕头",
parent_id: 4406,
sort: 0,
},
{
id: 4543,
name: "烧水壶",
parent_id: 4406,
sort: 0,
},
{
id: 4552,
name: "金色烧水壶",
parent_id: 4543,
sort: 0,
},
{
id: 4553,
name: "白色烧水壶",
parent_id: 4543,
},
{
id: 4512,
name: "2121",
parent_id: 4399,
},
{
id: 4406,
name: "生活用品",
parent_id: 0,
},
{
id: 4399,
name: "五金",
parent_id: 0,
},
{
id: 3955,
name: "家电",
parent_id: 0,
},
{
id: 3962,
name: "水果",
},
];
console.log(data);
let datas = [];
function getListTree(data) {
for (let i = 0; i < data.length; i++) {
if (data[i].parent_id == 0) {
datas.push(data[i]);
check(datas[datas.length - 1], data);
}
}
}
function check(dataInner, data) {
for (let i = 0; i < data.length; i++) {
if (dataInner.id == data[i].parent_id) {
dataInner.children = dataInner.children || [];
dataInner.children.push(data[i]);
check(dataInner.children[dataInner.children.length - 1], data);
}
}
}
getListTree(data);
console.log(datas);
</script>