假设数据结构为:
let data = [
{id: 1, text: 't11', parentId: 0},
{id: 2, text: 't11', parentId: 0},
{id: 3, text: 't11', parentId: 1},
{id: 4, text: 't11', parentId: 1},
{id: 5, text: 't11', parentId: 3},
{id: 6, text: 't11', parentId: 2},
];
需要转换成:
// let tree =[{
// id:1,text:'t11',parentId:0,
// child:[...]
// }]
计算代码:
data.forEach(ele => {
let parentId = ele.parentId;
if (parentId === 0) {
//是根元素的hua ,不做任何操作,如果是正常的for-i循环,可以直接continue.
} else {
//如果ele是子元素的话 ,把ele扔到他的父亲的child数组中.
data.forEach(d => {
if (d.id === parentId) {
let childArray = d.child;
if (!childArray) {
childArray = []
}
childArray.push(ele);
d.child = childArray;
}
})
}
});
//去除重复元素
data = data.filter(ele => ele.parentId === 0);
console.log('最终等到的tree结构数据: ', data);
评论 (0)