Tree structure data into a flat data

Taikang here to render a tree structure is flat, I wrote a method to flat data into a tree structure, now backstage pass the time, but also a flat background, can only write method

Data is structured as follows:

   let data = [
      {id:0,text:0,parentId:0,children:[
        {id:1,text:1,parentId:1,children:[
          {id:3,text:3,parentId:3,children:false}
        ]},
        {id:2,text:2,parentId:2,children:false}
      ]},
      {id:6,text:6,parentId:6,children:false}
    ]

Conversion Functions

 function  treeToPath(tree){
      let queen = [...tree];
      let result = [];
      while(queen.length){
        let first = queen.shift();
        if(first.children.length>0){
          queen = queen.concat(first.children)
          first['children'] = true;
        }
        result.push(first)
      }
      return result
    }

Finally, data converted into the following

Guess you like

Origin www.cnblogs.com/zpxm/p/11098470.html