方法一
/**
* 构造树型结构数据
* @param {*} data 数据源
* @param {*} id id字段 默认 'id'
* @param {*} parentId 父节点字段 默认 'parentId'
* @param {*} children 孩子节点字段 默认 'children'
*/
export function handleTree(data, id, parentId, children) {
const config = {
id: id || "id",
parentId: parentId || "parentId",
childrenList: children || "children",
};
var childrenListMap = {};
var nodeIds = {};
var tree = [];
for (const d of data) {
const parentId = d[config.parentId];
if (childrenListMap[parentId] == null) {
childrenListMap[parentId] = [];
}
nodeIds[d[config.id]] = d;
childrenListMap[parentId].push(d);
}
for (const d of data) {
const parentId = d[config.parentId];
if (nodeIds[parentId] == null) {
tree.push(d);
}
}
for (const t of tree) {
adaptToChildrenList(t);
}
function adaptToChildrenList(o) {
if (childrenListMap[o[config.id]] !== null) {
o[config.childrenList] = childrenListMap[o[config.id]];
}
if (o[config.childrenList]) {
for (const c of o[config.childrenList]) {
adaptToChildrenList(c);
}
}
}
return tree;
}
方法二
/**
* @Event 数组转树结构
* @description: pid: parentId
* @author: mhf
* @time: 2023-11-01 16:06:18
**/
export function arrayToTree(
list,
callback = () => {},
props = { id: 'id', pid: 'pid', children: 'children' }
) {
function sortArr(a, b) {
return a.orderNum - b.orderNum;
}
list.sort(sortArr);
const tree = [];
const map = {};
const listLength = list.length;
for (let i = 0; i < listLength; i++) {
const node = list[i];
const nodeId = node[props.id];
map[nodeId] = node;
callback(node);
}
for (let i = 0; i < listLength; i++) {
const node = list[i];
const nodePid = node[props.pid];
const parentNode = map[nodePid];
if (parentNode) {
parentNode[props.children] = parentNode[props.children] || [];
parentNode[props.children].push(node);
} else {
tree.push(node);
}
}
return tree;
}
原数组
[
{
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null,
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 21,
"parentId": 1,
"columnName": "政策分类",
"columnPath": null,
"imagesUrl": null,
"columnType": null,
"nodeType": "2",
"sortNum": 1,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null,
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 22,
"parentId": 21,
"columnName": "国家级",
"columnPath": null,
"imagesUrl": null,
"columnType": null,
"nodeType": "2",
"sortNum": 1,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 1,
"parentId": 0,
"columnName": "政策中心",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 1,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 5,
"parentId": 1,
"columnName": "政策发布",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 1,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 7,
"parentId": 2,
"columnName": "政策文件",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 1,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 11,
"parentId": 3,
"columnName": "展会活动",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 1,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 15,
"parentId": 4,
"columnName": "政策下载",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 1,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null,
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 23,
"parentId": 21,
"columnName": "省级",
"columnPath": null,
"imagesUrl": null,
"columnType": null,
"nodeType": "2",
"sortNum": 2,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 2,
"parentId": 0,
"columnName": "项目申报",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 2,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 6,
"parentId": 1,
"columnName": "政策解读",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 2,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 8,
"parentId": 2,
"columnName": "政策解读",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 2,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 12,
"parentId": 3,
"columnName": "产品汇聚",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 2,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 16,
"parentId": 4,
"columnName": "解读下载",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 2,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null,
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 24,
"parentId": 21,
"columnName": "市级",
"columnPath": null,
"imagesUrl": null,
"columnType": null,
"nodeType": "2",
"sortNum": 3,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 3,
"parentId": 0,
"columnName": "供需中心",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 3,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 9,
"parentId": 2,
"columnName": "项目申报",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 3,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 13,
"parentId": 3,
"columnName": "企业之窗",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 3,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": null,
"createTime": null,
"updateBy": null,
"updateTime": null,
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 25,
"parentId": 21,
"columnName": "其他",
"columnPath": null,
"imagesUrl": null,
"columnType": null,
"nodeType": "2",
"sortNum": 4,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 4,
"parentId": 0,
"columnName": "资料下载",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 4,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 10,
"parentId": 2,
"columnName": "项目公示",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 4,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
},
{
"createBy": "admin",
"createTime": "2023-10-23 02:04:47",
"updateBy": "admin",
"updateTime": "2023-10-23 02:04:47",
"beginTime": null,
"endTime": null,
"askParam": {},
"formSpan": null,
"id": 14,
"parentId": 3,
"columnName": "需求汇聚",
"columnPath": "",
"imagesUrl": "",
"columnType": null,
"nodeType": "1",
"sortNum": 4,
"status": 1,
"deleted": null,
"remark": null,
"columnList": null
}
]
处理之后的数组
[{
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 1,
'parentId': 0,
'columnName': '政策中心',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 1,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null,
'child': [{
'createBy': null,
'createTime': null,
'updateBy': null,
'updateTime': null,
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 21,
'parentId': 1,
'columnName': '政策分类',
'columnPath': null,
'imagesUrl': null,
'columnType': null,
'nodeType': '2',
'sortNum': 1,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null,
'child': [{
'createBy': null,
'createTime': null,
'updateBy': null,
'updateTime': null,
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 22,
'parentId': 21,
'columnName': '国家级',
'columnPath': null,
'imagesUrl': null,
'columnType': null,
'nodeType': '2',
'sortNum': 1,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': null,
'createTime': null,
'updateBy': null,
'updateTime': null,
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 23,
'parentId': 21,
'columnName': '省级',
'columnPath': null,
'imagesUrl': null,
'columnType': null,
'nodeType': '2',
'sortNum': 2,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': null,
'createTime': null,
'updateBy': null,
'updateTime': null,
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 24,
'parentId': 21,
'columnName': '市级',
'columnPath': null,
'imagesUrl': null,
'columnType': null,
'nodeType': '2',
'sortNum': 3,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': null,
'createTime': null,
'updateBy': null,
'updateTime': null,
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 25,
'parentId': 21,
'columnName': '其他',
'columnPath': null,
'imagesUrl': null,
'columnType': null,
'nodeType': '2',
'sortNum': 4,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}]
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 5,
'parentId': 1,
'columnName': '政策发布',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 1,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 6,
'parentId': 1,
'columnName': '政策解读',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 2,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}]
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 2,
'parentId': 0,
'columnName': '项目申报',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 2,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null,
'child': [{
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 7,
'parentId': 2,
'columnName': '政策文件',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 1,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 8,
'parentId': 2,
'columnName': '政策解读',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 2,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 9,
'parentId': 2,
'columnName': '项目申报',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 3,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 10,
'parentId': 2,
'columnName': '项目公示',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 4,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}]
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 3,
'parentId': 0,
'columnName': '供需中心',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 3,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null,
'child': [{
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 11,
'parentId': 3,
'columnName': '展会活动',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 1,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 12,
'parentId': 3,
'columnName': '产品汇聚',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 2,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 13,
'parentId': 3,
'columnName': '企业之窗',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 3,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 14,
'parentId': 3,
'columnName': '需求汇聚',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 4,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}]
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 4,
'parentId': 0,
'columnName': '资料下载',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 4,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null,
'child': [{
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 15,
'parentId': 4,
'columnName': '政策下载',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 1,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}, {
'createBy': 'admin',
'createTime': '2023-10-23 02:04:47',
'updateBy': 'admin',
'updateTime': '2023-10-23 02:04:47',
'beginTime': null,
'endTime': null,
'askParam': {},
'formSpan': null,
'id': 16,
'parentId': 4,
'columnName': '解读下载',
'columnPath': '',
'imagesUrl': '',
'columnType': null,
'nodeType': '1',
'sortNum': 2,
'status': 1,
'deleted': null,
'remark': null,
'columnList': null
}]
}
]