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

2022/6/24 14:21:54

本文主要是介绍Nodejs:(借鉴)列表数据结构,转换成树状结构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

借鉴内容:

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

  

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:(借鉴)列表数据结构,转换成树状结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程