将列表类型的数据转换为树形数据

列表的数据

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 }]



/**
 * 将列表数据转换为树形数据
 */
export 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 ,"")

猜你喜欢

转载自blog.csdn.net/weixin_46611729/article/details/114268894
今日推荐