function buildTree(arr, parentId = null) {
  let tree = [];
  for (let i = 0; i < arr.length; i++) {
    let item = arr[i];
    if (item.parentId === parentId) {
      let children = buildTree(arr, item.id);
      if (children.length) {
        item.children = children;
      }
      tree.push(item);
    }
  }
  return tree;
}

  

posted on 2023-03-27 09:15  chenlw101  阅读(22)  评论(0编辑  收藏  举报