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

发布时间 2023-12-22 16:31:45作者: SKa-M
点击查看代码
/**
 * 移动文件夹 (不能移动当前文件夹以及子文件夹)
 * @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 (node.id === 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;
      }
    }

    result.push(node);
  }

  return result;
}