将数据处理成树结构

const userGroups = [
  {
    corgId: 85,
    aorgId: 54,
    corgName: "lcygs",
    corgParent: 0,
    encode: "A1062",
    creationUserId: 54,
    creationUserId2: "sys_54",
    serialNumber: 0,
    corgLevel: 0,
  },
  {
    corgId: 89,
    aorgId: 54,
    corgName: "一级组织",
    corgParent: 85,
    encode: "A10620001",
    creationUserId: 86,
    creationUserId2: "company_86",
    serialNumber: 0,
    corgLevel: 1,
  },
  {
    corgId: 90,
    aorgId: 54,
    corgName: "二级组织",
    corgParent: 89,
    encode: "A106200010001",
    creationUserId: 86,
    creationUserId2: "company_86",
    serialNumber: 0,
    corgLevel: 2,
  },
  {
    corgId: 91,
    aorgId: 54,
    corgName: "三级组织",
    corgParent: 90,
    encode: "A1062000100010001",
    creationUserId: 86,
    creationUserId2: "company_86",
    serialNumber: 0,
    corgLevel: 3,
  },
  {
    corgId: 98,
    aorgId: 54,
    corgName: "总组织",
    corgParent: 85,
    encode: "A10620002",
    creationUserId: 86,
    creationUserId2: "company_86",
    serialNumber: 0,
    corgLevel: 1,
  },
];

const userMember = [
  {
    userId: 12476,
    userType: 3,
    userName: "dp4@lcygs.lcy",
    userCompanyid: 85,
    userAccount: "dp4@lcygs.lcy",
    battery: "100",
    signalValue: "10",
    userGpsswitch: 0,
    userGpsfrequency: 30,
  },
  {
    userId: 12469,
    userType: 0,
    userName: "ptt1@lcygs.lcy",
    userCompanyid: 89,
    userAccount: "ptt1@lcygs.lcy",
    battery: "85",
    signalValue: "3",
    userGpsswitch: 1,
    userGpsfrequency: 6,
  },
  {
    userId: 12470,
    userType: 0,
    userName: "ptt2@lcygs.lcy",
    userCompanyid: 89,
    userAccount: "ptt2@lcygs.lcy",
    battery: null,
    signalValue: null,
    userGpsswitch: 1,
    userGpsfrequency: 30,
  },
  {
    userId: 12471,
    userType: 0,
    userName: "ptt3@lcygs.lcy",
    userCompanyid: 89,
    userAccount: "ptt3@lcygs.lcy",
    battery: null,
    signalValue: null,
    userGpsswitch: 1,
    userGpsfrequency: 30,
  },
  {
    userId: 12474,
    userType: 3,
    userName: "dp2@lcygs.lcy",
    userCompanyid: 90,
    userAccount: "dp2@lcygs.lcy",
    battery: "97",
    signalValue: "3",
    userGpsswitch: 1,
    userGpsfrequency: 30,
  },
  {
    userId: 12473,
    userType: 3,
    userName: "dp1@lcygs.lcy",
    userCompanyid: 90,
    userAccount: "dp1@lcygs.lcy",
    battery: "100",
    signalValue: "10",
    userGpsswitch: 1,
    userGpsfrequency: 30,
  },
  {
    userId: 12475,
    userType: 3,
    userName: "dp3@lcygs.lcy",
    userCompanyid: 91,
    userAccount: "dp3@lcygs.lcy",
    battery: "100",
    signalValue: "10",
    userGpsswitch: 1,
    userGpsfrequency: 30,
  },
  {
    userId: 12472,
    userType: 0,
    userName: "ptt4@lcygs.lcy",
    userCompanyid: 91,
    userAccount: "ptt4@lcygs.lcy",
    battery: "86",
    signalValue: "4",
    userGpsswitch: 1,
    userGpsfrequency: 30,
  },
  {
    userId: 12492,
    userType: 0,
    userName: "ptt@lcygs.lcy",
    userCompanyid: 98,
    userAccount: "ptt@lcygs.lcy",
    battery: null,
    signalValue: null,
    userGpsswitch: 0,
    userGpsfrequency: 30,
  },
];

const emptyObj = {};

/**
 * [
 *  { corgId: 86, corgName: "一级组织", corgParent: 86, corgLevel: 1 },
 *  { corgId: 89, corgName: "二级组织", corgParent: 0, corgLevel: 0 },
 * ]
 * 
 * 先遍历到子集 后遍历到父级
 * 
 * 先遍历到父级 后遍历到子集
 */

const userListTree = userGroups.reduce((res, item) => {
   if (item.corgLevel === 0) {
     if (emptyObj[item.corgId]) {
       item.children = emptyObj[item.corgId].children;
       emptyObj[item.corgId] = item;
     }

     res.push(item);
   } else if (emptyObj[item.corgParent]) {
     emptyObj[item.corgParent].children = [
       item,
       ...(emptyObj[item.corgParent].children || []),
     ];
   } else {
     emptyObj[item.corgParent] = {
       children: [item],
     };
   }

   emptyObj[item.corgId] = item;

   item.uogPriority = null;
   const userMbers = userMember.filter(
     (_item) => _item.userCompanyid === item.corgId
   );
   console.error('userMbers',userMbers);
   if (userMbers) {
     userMbers.uogPriority = null;
     item.children = [...(item.children || []), ...userMbers];
   }
   return res;
 }, []);

console.log(userListTree);

userListTree 输出的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值