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 输出的数据