树形结构数据,进行过滤指定节点,再统计指定类型的节点数量,应该怎么弄啊?

liuchengyong
  • 53

有一树形结构如下:

const data = [
  {
    id: "xxxx",
    title: "中国xxxx有限公司",
    type: "RootCompany",
    children: [
      {
        id: "xxxxx",
        title: "本级各部门",
        type: "Rootdepartment",
        children: [
          {
            id: "xxxx",
            title: "人力资源中心",
            children: [
              {
                id: "xxxxx",
                title: "张三",
                type: "people",
              },
              {
                id: "xxxx",
                title: "李四",
                type: "people",
              },
            ],
          },
          {
            id: "xxxx",
            title: "战略投资部",
            type: "department",
          },
        ],
      },
      {
        id: "xxxx",
        title: "中国sxd有限公司",
        type: "company",
        children: [
          {
            id: "xxxx",
            title: "本级各部门",
            type: "department",
            children: [
              {
                id: "xxxx",
                title: "企业领导",
                type: "department",
                children: [
                  {
                    id: "xxxxx",
                    title: "李白",
                    type: "people",
                  },
                ],
              },
              {
                id: "xxxx",
                title: "人力资源中心",
                type: "department",
              },
            ],
          },
        ],
      },
      {
        id: "xxxx",
        title: "中国lsd有限公司",
        type: "company",
        children: [
          {
            id: "xxxx",
            title: "本级各部门",
            type: "Rootdepartment",
            children: [
              {
                id: "xxxx",
                title: "公司领导",
                type: "department",
              },
            ],
          },
          {
            id: "xxxx",
            title: "中国oosd有限公司",
            type: "company",
            children: [
              {
                id: "xxxx",
                title: "本级各部门",
                type: "Rootdepartment",
                children: [
                  {
                    id: "xxxx",
                    title: "公司领导",
                    type: "department",
                    children: [
                      {
                        id: "xxxx",
                        title: "孙悟空",
                        type: "people",
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
];

像上面这种形式的树形结构,我现在需要在每个type为company或Rootcompany的节点上统计其下子节点type为company或Rootcompany的数量,用childCount属性表示该节点的子节点个数.这个应该怎么弄啊?那位大佬帮帮忙.(就是在进行子节点数量统计时,不要将type为department和Rootdepartment和people的节点计算进去).

回复
阅读 236
5 个回答

只能统计连续父节点的

const data = [
  {
    id: "xxxx",
    title: "中国xxxx有限公司",
    type: "RootCompany",
    children: [
      {
        id: "xxxxx",
        title: "本级各部门",
        type: "Rootdepartment",
        children: [
          {
            id: "xxxx",
            title: "人力资源中心",
            children: [
              {
                id: "xxxxx",
                title: "张三",
                type: "people",
              },
              {
                id: "xxxx",
                title: "李四",
                type: "people",
              },
            ],
          },
          {
            id: "xxxx",
            title: "战略投资部",
            type: "department",
          },
        ],
      },
      {
        id: "xxxx",
        title: "中国sxd有限公司",
        type: "company",
        children: [
          {
            id: "xxxx",
            title: "本级各部门",
            type: "department",
            children: [
              {
                id: "xxxx",
                title: "企业领导",
                type: "department",
                children: [
                  {
                    id: "xxxxx",
                    title: "李白",
                    type: "people",
                  },
                ],
              },
              {
                id: "xxxx",
                title: "人力资源中心",
                type: "department",
              },
            ],
          },
        ],
      },
      {
        id: "xxxx",
        title: "中国lsd有限公司",
        type: "company",
        children: [
          {
            id: "xxxx",
            title: "本级各部门",
            type: "Rootdepartment",
            children: [
              {
                id: "xxxx",
                title: "公司领导",
                type: "department",
              },
            ],
          },
          {
            id: "xxxx",
            title: "中国oosd有限公司",
            type: "company",
            children: [
              {
                id: "xxxx",
                title: "本级各部门",
                type: "Rootdepartment",
                children: [
                  {
                    id: "xxxx",
                    title: "公司领导",
                    type: "department",
                    children: [
                      {
                        id: "xxxx",
                        title: "孙悟空",
                        type: "people",
                      },
                    ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
];
// 设置指定节点的统计字段,并返回指定节点总数
var toTreeCount = (data=[], countField='count',countType=[])=>data.reduce((total,cur)=>(total+(countType.includes(cur.type) ? (cur[countField] = toTreeCount(cur.children||[], countField,countType) + 1) : total)),0);
console.log(toTreeCount(data,'childCount',['company','RootCompany']),data);

image.png

在线体验地址

type TreeNode = {
  id: string;
  title: string;
  type?: string;
  children?: TreeNode[];
};

type TreeNodeWithExtraInfo = TreeNode & {
  company?: number;
  Rootcompany?: number;
  children?: TreeNodeWithExtraInfo[];
};


const mergeInfoFormSubTreeToRoot = (nodes: TreeNodeWithExtraInfo[]) => {
  const dummyRoot: TreeNodeWithExtraInfo = {
    children: nodes,
    company: 0,
    Rootcompany: 0
  } as any;

  const mergeUp = (node: TreeNodeWithExtraInfo): void => {
    if (!node.children) return;
    for (const child of node.children) {
      mergeUp(child);

      if (child.type === "company") {
        child.company = 1;
        child.Rootcompany = 0;
      } else if (child.type === "RootCompany") {
        child.Rootcompany = 1;
        child.company = 0;
      } else {
        child.company = child.Rootcompany = 0;
      }

      for (const x of child.children || []) {
        child.company += x.company;
        child.Rootcompany += x.Rootcompany;
      }
    }
  };

  mergeUp(dummyRoot);

  for (const x of dummyRoot.children || []) {
    dummyRoot.company += x.company;
    dummyRoot.Rootcompany += x.Rootcompany;
  }

  return dummyRoot;
};

console.log(mergeInfoFormSubTreeToRoot(data));
TorTorMore
  • 3
新手上路,请多包涵

随手撸的,没有测试过

function tree (arr) {
  if (!Array.isArray(arr)) return;
  arr.forEach((item, index) => {
    const strList = ['company', 'Rootcompany']
    if (strList.includes(item.type)) {
      arr[index].childCount = 0
      const callBack = (node) => {
        if (strList.includes(node.type)) {
          arr[index].childCount += 1
          node.childCount = 0
          if (node.children.length) {
            tree(node.children)
          }
        }
      }
      if (item.children.length) {
        callBack(item.children)
      }
    }
  })
}

原理就是通过递归去修改原数组,保持原数组长度不变

这不应该是你们接口仔来做?

将tree拉平,每项增加pType,然后循环这个拉平后的数组计算数量

你知道吗?

宣传栏