javascript將樹結構的列表轉換為普通列表(不帶children)


在用一些樹表插件的時候,這些樹表插件都會要求數據的機構是帶children屬性的對象數組,而保存的時候則可能需要在前端轉換為普通的列表。

  function treeListToList(treeList) { // 將樹結構的列表轉換為普通列表
    let list = [];
    handleTreeList(treeList, list);
    return list
  }
function handleTreeList(treeList, list) {
  if (!treeList || !treeList.length) {
    return
  }
  for (let i = 0; i < treeList.length; i++) {
    let currentRow = treeList[i];
    let newRow = JSON.parse(JSON.stringify(currentRow));
    newRow.children = undefined;
    list.push(newRow);
    handleTreeList(currentRow.children, list)
  }
}

封裝成函數,方便多次調用。

 

"一個有趣的悖論是,當我接受自己原本的樣子時,我就能改變了。"


免責聲明!

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



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