var arr = [
{id:1,name:'aaa1'},
{id:2,name:'aaa2',parentId:1},
{id:3,name:'aaa3'},
{id:4,name:'aaa4',parentId:2},
{id:5,name:'aaa5'},
{id:6,name:'aaa6',parentId:4},
]
var getStruct = (arr)=>{
if(arr.length === 0)return arr
let idArr = []
let parentIdArr = []
arr.forEach(element => {
if(element.parentId){
parentIdArr.push(element)
}
});
arr.forEach(v=>{
parentIdArr.forEach(idv=>{
if(v.id === idv.parentId){
v.children = idv
}
})
})
for(let i = 0;i<arr.length;i++){
if(arr[i].parentId){
arr.splice(i,1)
}
}
console.log(arr)
}
getStruct(arr)