算法来源​​这个作者​

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var arr = [
{
caption: "系统资源",
remark: "resource",
id: 1,
parentId: -1,
},
{
caption: "资源动作",
remark: "resource",
id: 2,
parentId: -1,
},
{
caption: "资源数据",
remark: "resource",
id: 3,
parentId: -1,
},
{
caption: "资源字段",
remark: "resource",
id: 4,
parentId: -1,
},
{
caption: "租户信息",
remark: "resource",
id: 5,
parentId: -1,
},
{
caption: "菜单管理",
remark: "resource",
id: 6,
parentId: -1,
},
{
caption: "角色管理",
remark: "resource",
id: 7,
parentId: -1,
},
{
caption: "部门管理",
remark: "resource",
id: 8,
parentId: -1,
},
{
caption: "字典操作",
remark: "resource",
id: 9,
parentId: -1,
},
{
caption: "用户管理",
remark: "resource",
id: 10,
parentId: -1,
},
{
caption: "日志操作",
remark: "resource",
id: 11,
parentId: -1,
},
{
caption: "任务管理",
remark: "resource",
id: 12,
parentId: -1,
},
{
caption: "调度日志",
remark: "resource",
id: 13,
parentId: -1,
},
{
caption: "缓存操作",
remark: "resource",
id: 14,
parentId: -1,
},
{
caption: "修改系统资源数据",
remark: "action",
id: 47,
parentId: 1,
},
{
caption: "删除系统资源数据",
remark: "action",
id: 48,
parentId: 1,
},
{
caption: "新增系统资源数据",
remark: "action",
id: 49,
parentId: 1,
},
{
caption: "查询系统资源列表-分页数据",
remark: "action",
id: 50,
parentId: 1,
},
{
caption: "查询系统资源列表-数据记录数",
remark: "action",
id: 51,
parentId: 1,
},
{
caption: "查询系统资源数据详情",
remark: "action",
id: 52,
parentId: 1,
},
{
caption: "修改系统资源操作数据",
remark: "action",
id: 41,
parentId: 2,
},
{
caption: "删除系统资源操作数据",
remark: "action",
id: 42,
parentId: 2,
},
{
caption: "新增系统资源操作数据",
remark: "action",
id: 43,
parentId: 2,
},
{
caption: "查询系统资源操作-分页数据",
remark: "action",
id: 44,
parentId: 2,
},
{
caption: "查询系统资源操作-数据记录数",
remark: "action",
id: 45,
parentId: 2,
},
{
caption: "查询系统资源操作详情",
remark: "action",
id: 46,
parentId: 2,
},
{
caption: "修改系统资源数据操作数据",
remark: "action",
id: 53,
parentId: 3,
},
{
caption: "删除系统资源数据操作数据",
remark: "action",
id: 54,
parentId: 3,
},
{
caption: "新增系统资源数据操作数据",
remark: "action",
id: 55,
parentId: 3,
},
{
caption: "查询系统资源数据操作-分页数据",
remark: "action",
id: 56,
parentId: 3,
},
{
caption: "查询系统资源数据操作-数据记录数",
remark: "action",
id: 57,
parentId: 3,
},
{
caption: "查询系统资源数据操作详情",
remark: "action",
id: 58,
parentId: 3,
},
{
caption: "修改系统资源字段操作数据",
remark: "action",
id: 59,
parentId: 4,
},
{
caption: "删除系统资源字段操作数据",
remark: "action",
id: 60,
parentId: 4,
},
{
caption: "批量新增系统资源字段操作数据",
remark: "action",
id: 61,
parentId: 4,
},
{
caption: "新增系统资源字段操作数据",
remark: "action",
id: 62,
parentId: 4,
},
{
caption: "查询系统资源字段操作-分页数据",
remark: "action",
id: 63,
parentId: 4,
},
{
caption: "查询系统资源字段操作-数据记录数",
remark: "action",
id: 64,
parentId: 4,
},
{
caption: "查询系统资源字段操作-根据dataClass获取所有属性信息",
remark: "action",
id: 65,
parentId: 4,
},
{
caption: "查询系统资源字段操作详情",
remark: "action",
id: 66,
parentId: 4,
},
{
caption: "禁用租户",
remark: "action",
id: 85,
parentId: 5,
},
{
caption: "启用租户",
remark: "action",
id: 86,
parentId: 5,
},
{
caption: "修改租户数据",
remark: "action",
id: 87,
parentId: 5,
},
{
caption: "删除租户管理数据",
remark: "action",
id: 88,
parentId: 5,
},
{
caption: "新增租户数据",
remark: "action",
id: 89,
parentId: 5,
},
{
caption: "查询租户列表-分页数据",
remark: "action",
id: 90,
parentId: 5,
},
{
caption: "查询租户列表-数据记录数",
remark: "action",
id: 91,
parentId: 5,
},
{
caption: "查询租户数据详情",
remark: "action",
id: 92,
parentId: 5,
},
{
caption: "修改菜单数据",
remark: "action",
id: 34,
parentId: 6,
},
{
caption: "删除菜单管理数据",
remark: "action",
id: 35,
parentId: 6,
},
{
caption: "新增菜单数据",
remark: "action",
id: 36,
parentId: 6,
},
{
caption: "查询菜单列表-分页数据",
remark: "action",
id: 37,
parentId: 6,
},
{
caption: "查询父菜单列表",
remark: "action",
id: 38,
parentId: 6,
},
{
caption: "查询菜单列表-数据记录数",
remark: "action",
id: 39,
parentId: 6,
},
{
caption: "查询菜单数据详情",
remark: "action",
id: 40,
parentId: 6,
},
{
caption: "修改角色数据",
remark: "action",
id: 67,
parentId: 7,
},
{
caption: "删除角色管理数据",
remark: "action",
id: 68,
parentId: 7,
},
{
caption: "新增角色数据",
remark: "action",
id: 69,
parentId: 7,
},
{
caption: "查询绑定资源字段",
remark: "action",
id: 70,
parentId: 7,
},
{
caption: "查询角色列表-分页数据",
remark: "action",
id: 71,
parentId: 7,
},
{
caption: "查询角色列表-数据记录数",
remark: "action",
id: 72,
parentId: 7,
},
{
caption: "查询角色数据详情",
remark: "action",
id: 73,
parentId: 7,
},
{
caption: "查询绑定菜单",
remark: "action",
id: 74,
parentId: 7,
},
{
caption: "绑定菜单",
remark: "action",
id: 75,
parentId: 7,
},
{
caption: "绑定资源动作",
remark: "action",
id: 76,
parentId: 7,
},
{
caption: "查询绑定资源动作",
remark: "action",
id: 77,
parentId: 7,
},
{
caption: "绑定资源字段",
remark: "action",
id: 78,
parentId: 7,
},
{
caption: "修改部门数据",
remark: "action",
id: 1,
parentId: 8,
},
{
caption: "删除部门管理数据",
remark: "action",
id: 2,
parentId: 8,
},
{
caption: "新增部门数据",
remark: "action",
id: 3,
parentId: 8,
},
{
caption: "查询部门列表-分页数据",
remark: "action",
id: 4,
parentId: 8,
},
{
caption: "查询父部门列表",
remark: "action",
id: 5,
parentId: 8,
},
{
caption: "查询部门列表-数据记录数",
remark: "action",
id: 6,
parentId: 8,
},
{
caption: "查询已经绑定的媒介平台列表",
remark: "action",
id: 7,
parentId: 8,
},
{
caption: "查询部门数据详情",
remark: "action",
id: 8,
parentId: 8,
},
{
caption: "修改字典数据",
remark: "action",
id: 9,
parentId: 9,
},
{
caption: "删除字典管理数据",
remark: "action",
id: 10,
parentId: 9,
},
{
caption: "新增字典数据",
remark: "action",
id: 11,
parentId: 9,
},
{
caption: "查询字典列表-分页数据",
remark: "action",
id: 12,
parentId: 9,
},
{
caption: "查询字典列表-数据记录数",
remark: "action",
id: 13,
parentId: 9,
},
{
caption: "查询字典数据详情",
remark: "action",
id: 14,
parentId: 9,
},
{
caption: "禁用用户帐号",
remark: "action",
id: 93,
parentId: 10,
},
{
caption: "启用用户帐号",
remark: "action",
id: 94,
parentId: 10,
},
{
caption: "锁定用户帐号",
remark: "action",
id: 95,
parentId: 10,
},
{
caption: "修改用户数据",
remark: "action",
id: 96,
parentId: 10,
},
{
caption: "删除用户管理数据",
remark: "action",
id: 97,
parentId: 10,
},
{
caption: "新增用户数据",
remark: "action",
id: 98,
parentId: 10,
},
{
caption: "解锁用户帐号",
remark: "action",
id: 99,
parentId: 10,
},
{
caption: "解锁用户帐号",
remark: "action",
id: 100,
parentId: 10,
},
{
caption: "查询用户数据详情",
remark: "action",
id: 101,
parentId: 10,
},
{
caption: "查询用户数据详情",
remark: "action",
id: 102,
parentId: 10,
},
{
caption: "查询用户列表-分页数据",
remark: "action",
id: 103,
parentId: 10,
},
{
caption: "查询用户列表-数据记录数",
remark: "action",
id: 104,
parentId: 10,
},
{
caption: "查询绑定角色",
remark: "action",
id: 105,
parentId: 10,
},
{
caption: "查询绑定部门",
remark: "action",
id: 106,
parentId: 10,
},
{
caption: "绑定部门",
remark: "action",
id: 107,
parentId: 10,
},
{
caption: "绑定角色",
remark: "action",
id: 108,
parentId: 10,
},
{
caption: "删除日志管理数据",
remark: "action",
id: 29,
parentId: 11,
},
{
caption: "新增日志数据",
remark: "action",
id: 30,
parentId: 11,
},
{
caption: "查询日志列表-分页数据",
remark: "action",
id: 31,
parentId: 11,
},
{
caption: "查询日志列表-数据记录数",
remark: "action",
id: 32,
parentId: 11,
},
{
caption: "查询日志数据详情",
remark: "action",
id: 33,
parentId: 11,
},
{
caption: "启用任务",
remark: "action",
id: 16,
parentId: 12,
},
{
caption: "修改任务数据",
remark: "action",
id: 17,
parentId: 12,
},
{
caption: "删除任务管理数据",
remark: "action",
id: 18,
parentId: 12,
},
{
caption: "新增任务数据",
remark: "action",
id: 19,
parentId: 12,
},
{
caption: "查询任务状态枚举列表",
remark: "action",
id: 20,
parentId: 12,
},
{
caption: "查询任务列表-分页数据",
remark: "action",
id: 21,
parentId: 12,
},
{
caption: "查询任务列表-数据记录数",
remark: "action",
id: 22,
parentId: 12,
},
{
caption: "查询任务类型",
remark: "action",
id: 23,
parentId: 12,
},
{
caption: "查询任务数据详情",
remark: "action",
id: 24,
parentId: 12,
},
{
caption: "禁用任务",
remark: "action",
id: 25,
parentId: 12,
},
{
caption: "查询调度日志列表-分页数据",
remark: "action",
id: 26,
parentId: 13,
},
{
caption: "查询调度日志列表-数据记录数",
remark: "action",
id: 27,
parentId: 13,
},
{
caption: "查询调度日志数据详情",
remark: "action",
id: 28,
parentId: 13,
},
];
// 存在上下级关系的行数据


// 行结构数据转化为树形结构数据
function rowData2TreeData(rows,) {
const keyNodes = {}, parentKeyNodes = {};
for (let i = 0; i < rows.length; i++) {
let row = rows[i]; // 将行数据整体赋值后再做需要的处理
row.id = row[config.id];
row[config.pid] = row[config.pid];
row[config.children] = [];

keyNodes[row.id] = row;

if (parentKeyNodes[row[config.pid]]) { parentKeyNodes[row[config.pid]].push(row); }
else { parentKeyNodes[row[config.pid]] = [row]; }

let children = parentKeyNodes[row.id];
if (children) { row[config.children] = children; }

let pNode = keyNodes[row[config.pid]];
if (pNode) { pNode[config.children].push(row); }
}
return parentKeyNodes[config.rootKey];
}

// 属性配置信息
const config = {
id: 'id',// id字段
pid: 'parentId',// 上级id字段
children: 'children',// 生成的下级节点key字段
rootKey: '-1' // 根节点上级id
}
let data1 = rowData2TreeData(arr, config)
console.log(data1)
</script>
</head>

<body>

</body>

</html>