跳到主要内容

数组转树

// 例如将 input 转成output的形式
let input = [
{
id: 1,
val: "学校",
parentId: null,
},
{
id: 2,
val: "班级1",
parentId: 1,
},
{
id: 3,
val: "班级2",
parentId: 1,
},
{
id: 4,
val: "学生1",
parentId: 2,
},
{
id: 5,
val: "学生2",
parentId: 2,
},
{
id: 6,
val: "学生3",
parentId: 3,
},
];

let output = [
{
id: 1,
val: "学校",
children: [
{
id: 2,
val: "班级1",
children: [
{
id: 4,
val: "学生1",
children: [],
},
{
id: 5,
val: "学生2",
children: [],
},
],
},
{
id: 3,
val: "班级2",
children: [
{
id: 6,
val: "学生3",
children: [],
},
],
},
],
},
];

// 代码实现
function arrayToTree(array) {
let root = array[0];
return toTree(null, array);
}

// 传入父节点id,和没有根元素的array
function toTree(parentId, array) {
let children = [];
let len = array.length;
for (let i = 0; i < len; i++) {
let node = array[i];
if (node.parentId === parentId) {
children.push({
id: node.id,
val: node.val,
// 没有child,toTree会返回children初值[],所以不用担心
children: toTree(node.id, array),
});
}
}
return children;
}