我有一个对象数组,像这样。
let users = [
{
user :{
idUser: 2,
name: "Alain",
age: 23
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
{
user :{
idUser: 2,
name: "Alain",
age: 23
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 8,
nomGroup: 'Finance personnes'
}
},
{
user :{
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
{
user :{
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'GEST',
label: 'RH Helper'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
];
我想从用户对象中返回由用户id和角色对象中的代码角色摸索出来的对象数组,并为每个id用户和代码角色新建一个组的数组,像这样。
results = [
{
user :{
idUser: 2,
name: "Alain",
age: 23
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
groups: [{
idGroup: 3,
nomGroup: 'RH personnes'
},
{
idGroup: 8,
nomGroup: 'Finance personnes'
}]
},
{
user :{
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
groups: [{
idGroup: 3,
nomGroup: 'RH personnes'
}]
},
{
user :{
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'GEST',
label: 'RH Helper'
},
groups: [{
idGroup: 3,
nomGroup: 'RH personnes'
}]
},
];
我试过用reduce,但我找不到如何做到这一点?
谢谢你的帮助
使用 reduce
& findIndex
. 在 reduce 回调函数中使用 findIndex
来查找是否有任何对象存在于累加器中,并与该对象相同的 idUser
和 codeRole
. 如果它存在,那么更新 group
数组的值,并从组中推送。
如果它不存在,则创建新的对象,并推送它的值。group
阵列
let users = [{
user: {
idUser: 2,
name: "Alain",
age: 23
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
{
user: {
idUser: 2,
name: "Alain",
age: 23
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 8,
nomGroup: 'Finance personnes'
}
},
{
user: {
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
{
user: {
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'GEST',
label: 'RH Helper'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
];
let newData = users.reduce((acc, curr) => {
const isUserIncluded = acc.findIndex(item => curr.user.idUser === item.user.idUser && curr.role.codeRole === item.role.codeRole);
if (isUserIncluded === -1) {
const tempArray = [];
tempArray.push(curr.group)
acc.push({ ...curr,
group: tempArray
})
} else {
acc[isUserIncluded].group.push(curr.group)
}
return acc;
}, []);
console.log(newData)
像下面这样尝试。
在reduce函数里面。
注:在 reduce
第二个参数是你的返回对象。
let users = [{
user: {
idUser: 2,
name: "Alain",
age: 23
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
{
user: {
idUser: 2,
name: "Alain",
age: 23
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 8,
nomGroup: 'Finance personnes'
}
},
{
user: {
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'ADM',
label: 'Administrator'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
{
user: {
idUser: 8,
name: "Jhon",
age: 33
},
role: {
codeRole: 'GEST',
label: 'RH Helper'
},
group: {
idGroup: 3,
nomGroup: 'RH personnes'
}
},
];
function groupBy(r, u) {
// find if your group key already exists or not.
let obj = r.find(x => x.user.idUser == u.user.idUser && x.role.codeRole == u.role.codeRole);
// if not exist then create new object and add to return object.
if (!obj) {
obj = {
user: u.user,
role: u.role,
groups: []
};
r.push(obj);
}
// add group from current object to grouped object
obj.groups.push(u.group);
// return result object
return r;
}
// in reduce second parameter is your return object.
let result = [];
users.reduce(groupBy, result);
console.log(result);