ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Nodejs:(借鉴)列表数据结构,转换成树状结构

2022-06-23 09:37:29  阅读:159  来源: 互联网

标签:数据结构 树状 Nodejs pid introduce manager code NULL id


借鉴内容:

  • 列表数据结构转换成树状结构

  

const depts = [{ "id": "1175310929766055936", "pid": "", "companyId": "1", "name": "总裁办", "code": "ZCB1", "managerId": NULL, "manager": "孙财", "introduce": "总裁办公室", "createTime": NULL }, { "id": "1175311213774962688", "pid": "", "companyId": "1", "name": "人事部", "code": "XZB12f", "managerId": NULL, "manager": "罗小小", "introduce": "232\n31", "createTime": NULL }, { "id": "1175311267684352000", "pid": "", "companyId": "1", "name": "人事部bb", "code": "XZB1", "managerId": "1071632760222810112", "manager": "孙财", "introduce": "wede", "createTime": NULL }, { "id": "1175311325720936448", "pid": "", "companyId": "1", "name": "财务部", "code": "CWB", "managerId": NULL, "manager": "孙财", "introduce": "1111", "createTime": NULL }, { "id": "1175311373083017216", "pid": "", "companyId": "1", "name": "技术部", "code": "JSB", "managerId": NULL, "manager": "文吉星", "introduce": "n mmm", "createTime": NULL }, { "id": "1175311418004013056", "pid": "", "companyId": "1", "name": "运营部", "code": "YYB", "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1175311466846683136", "pid": "", "companyId": "1", "name": "市场部", "code": "SCB", "managerId": "1063705989926227968", "manager": "武高丽", "introduce": NULL, "createTime": NULL }, { "id": "1235395178363559936", "pid": "1175311325720936448", "companyId": "1", "name": "财务核算部", "code": NULL, "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1235398264104624128", "pid": "1175311325720936448", "companyId": "1", "name": "税务管理部", "code": NULL, "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1235398536969265152", "pid": "1175311325720936448", "companyId": "1", "name": "薪资管理部", "code": NULL, "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1235398608847052800", "pid": "1175311373083017216", "companyId": "1", "name": "Java研发部", "code": "1", "managerId": NULL, "manager": "管理员", "introduce": "1", "createTime": NULL }, { "id": "1235398661355544576", "pid": "1175311373083017216", "companyId": "1", "name": "Python研发部", "code": NULL, "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1235398708763762688", "pid": "1175311373083017216", "companyId": "1", "name": "Php研发部", "code": NULL, "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1235398917619130368", "pid": "1175311466846683136", "companyId": "1", "name": "北京事业部", "code": NULL, "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1235399011458293760", "pid": "1175311466846683136", "companyId": "1", "name": "上海事业部", "code": NULL, "managerId": NULL, "manager": NULL, "introduce": NULL, "createTime": NULL }, { "id": "1366242949844746240", "pid": "1175311418004013056", "companyId": "1", "name": "运营部", "code": "YYb", "managerId": NULL, "manager": "文吉星", "introduce": "12312312", "createTime": NULL }, { "id": "1366307215809495040", "pid": "1175310929766055936", "companyId": "1", "name": "re", "code": "wewe", "managerId": NULL, "manager": "董昊空", "introduce": "weee", "createTime": NULL }];

/**
 * 将列表数据转换为树形数据
 */
 function tranListToTreeDate (list, rootValue) {
  // 存放子节点的数据
  const arr = []
  // 遍历列表
  list.forEach(item => {
    // 如果当前项item的pid等于 roorValue,说明当前项item是rootValue的子节点(对象类型)
    if(item.pid === rootValue) {
      // 找到当前项的子节点,如果没有,则会返回一个空数组
      const children = tranListToTreeDate(list, item.id)
      // 如果数组不为空,则表示当前项有子节点,且所有子节点都在 children 中
      IF (children.length) {
        // 将当前项的所有子节点挂载到当前项的属性 children 下
        item.children = children;
      }
      // 将 rootValue 匹配的所有子节点放到 arr 中
      arr.push(item);
    }
  })
  // 返回一个数组,子节点的数据(如果当前项没有子节点,会返回一个空数组)
  return arr
}

const departs =  tranListToTreeDate(depts ,"");

 

标签:数据结构,树状,Nodejs,pid,introduce,manager,code,NULL,id
来源: https://www.cnblogs.com/EeDFanRen/p/16403984.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有