const result =convert(list);functionconvert(list){// your codelet childrens =newArray(list.length +1);let res =[];for(let i =0; i <= list.length; i++){
childrens[i]=[];}for(let item of list){
childrens[item.parentKey].push(item);}//初始化,parentKey = 0for(let item of childrens[0]){
res.push(item);}constdfs=(item)=>{if(childrens[item.key].length ===0)return;
item.children =[];for(let ch of childrens[item.key]){
item.children.push(ch);dfs(ch);}};for(let item of res){dfs(item);}return res;}