A js Recursive algorithm problem

1. An array of objects with tree structure
[{id:0},
{id:1,parentId:0},
{id:2},
{id:3,parentId:1},
{id:4,parentId:0},
{id:5,parentId:3}]

2. It is required to finish in
[{id:0},
{id:1,parentId:0},
{id:3,parentId:1},
{id:5,parentId:3}
{id:4,parentId:0},
{id:2}]
to explain: it is a depth-first traversal. Data with a parent node will have the attribute parentId. If a node has a child node (parentID== has its own id), it is directly inserted into his child node. Loop until the insert node has no child nodes. Those of the same class are arranged in previous order.

there are probably some ideas, but it is wrong to write for a long time. Ask God to give me a demo to learn

.
Mar.31,2021

https://jsfiddle.net/g7askt9w. can only write this first, which is very expensive if the data is very large

let datas =[{id:0},{id:2},
{id:1,parentId:0},
{id:6,parentId:2},
{id:7,parentId:6},
{id:3,parentId:1},
{id:4,parentId:0},
{id:5,parentId:3}]
let arr=[]
function treedata(data,a){
   data.forEach((r,index)=>{
      if(r.parentId==a.id){
        arr.push(r)
        /* datas.splice(index,1) */
        treedata(datas,r)
      }
       
   })
}
datas.forEach((r,index)=>{
   if(r.parentId || r.parentId===0){
   }else{
      arr.push(r)
      /* datas.splice(index,1) */
      treedata(datas,r) 
   }
})
console.log(arr) 

var array =[{id:0},
{id:1,parentId:0},
{id:2},
{id:3,parentId:1},
{id:4,parentId:0},
{id:5,parentId:3}];

//
for(var i = 0;i < array.length;iPP){
      var cur = array[i];
        for(var j = i+1;j < array.length;jPP){
            var next = array[j];
            if(cur.id === next.parentId){
                     if(!cur.nextIndexArray){
                                cur.nextIndexArray = [];
                         }
                      cur.nextIndexArray.push(j);
                    next.preIndex = i;
            }else if(cur.parentId === next.id){
                    if(!next.nextIndexArray){
                                next.nextIndexArray = [];
                         }
                      next.nextIndexArray.push(i);
                    cur.preIndex = j;
            }
        }
}
var result = [];
//
function recursiveTree(obj){
     result.push(obj);
      var nextIndexArray = obj.nextIndexArray;
      if(!nextIndexArray){
            return;
        }
      for(var i = 0;i < nextIndexArray.length;iPP){
               recursiveTree(array[nextIndexArray[i]]);
        }
}
for (var i = 0;i < array.length;iPP){
       if(typeof(array[i].preIndex) == "undefined"){
                recursiveTree(array[i]);
         }
}
console.log(result);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37223-2c074.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37223-2c074.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?