对树形结构过滤处理(过滤掉选中文件夹以及子级数据)

点击查看代码
/**
 * 移动文件夹 (不能移动当前文件夹以及子文件夹)
 * @param tree 树形结构
 * @param condition 过滤单条数据
 * @returns
 */
export function excludeNodeAndChildren(tree: any, condition: any) {
  if (!Array.isArray(tree) || typeof condition !== 'object') {
    return tree;
  }
  const result = [];

  for (const node of tree) {
    if (node.id === condition.id && node.pid === condition.pid) {
      continue;
    }

    if (Array.isArray(node.children) && node.children.length > 0) {
      const children = excludeNodeAndChildren(node.children, condition);
      if (children.length > 0) {
        node.children = children;
      } else {
        node.children = [];
      }
    }

    result.push(node);
  }

  return result;
}

  return result;
}
posted @ 2023-12-22 16:27  SKa-M  阅读(48)  评论(0编辑  收藏  举报