- id 自增ID
- pe: 上级ID
- title: 栏目名称
let jn = [
{ id: 1, pe: 0, title: "1" },
{ id: 2, pe: 0, title: "2" },
{ id: 3, pe: 1, title: "3" },
{ id: 4, pe: 2, title: "4" },
{ id: 5, pe: 1, title: "5" },
{ id: 6, pe: 0, title: "6" },
];
this.treeData(jn, "id", "pe", "children")
函数体,通过filter方法进行过渡处理
treeData(source, id, parentId, children) {
let cloneData = JSON.parse(JSON.stringify(source));
return cloneData.filter(father => {
// 顶级过滤
let branchArr = cloneData.filter(child => father[id] === child[parentId]);
// 子级过滤
branchArr.length > 0 ? father[children] = branchArr : "";
return father[parentId] === 0; // 如果第一层不是parentId=0,请自行修改
});
},