樹狀結構數據轉成扁平數據


泰康這邊渲染樹形結構給的是扁平的,我寫了個方法把扁平數據轉成樹結構,現在傳給后台時候,后台又要扁平的,只能在寫個方法

數據結構如下:

   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}
    ]

轉換函數

 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
    }

最后轉成的數據如下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM