简体   繁体   English

从由许多 object 属性分组的另一个对象返回数组

[英]return array of objects from another grouped by many object properties

I have an array of objects like this:我有一个这样的对象数组:

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'    
}
},
];

and I want to return this array of objects groped by user id from user object and code role from role object, with new key groups who has an array of groups for every id user and code role like this:我想返回这个由用户 object 的用户 id 和角色 object 的代码角色摸索的对象数组,新的密钥组具有每个 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'    
}]
},

]; ];

I tried with reduce but I can not find how to do that?我试过减少,但我找不到怎么做?

thanks for your help感谢您的帮助

Try like below.尝试如下。

Inside reduce function.内部减少 function。

  1. find if your group key already exists in result object or not.查找您的组密钥是否已存在于结果 object 中。
  2. if not exist then create new object and add to return object.如果不存在,则创建新的 object 并添加以返回 object。
  3. add group from current object to grouped object将当前 object 的组添加到分组的 object
  4. return result object返回结果 object

Note: In reduce second parameter is your return object.注意: reduce中的第二个参数是您的返回 object。

 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);

Use reduce & findIndex .使用reducefindIndex In the reduce callback function use findIndex to find if any object exists in the accumulator with same idUser and codeRole .在 reduce 回调 function 中,使用findIndex查找累加器中是否存在具有相同idUser和 codeRole 的codeRole If it exist then update the group array and push the value from group.如果存在,则更新group数组并从组中推送值。

If it does not exist then create new object and push the value of group in an array如果不存在,则创建新的 object 并将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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数组中的对象属性推入另一个对象数组 - Push object properties in an array into another array of objects 如何从对象数组的属性返回数组 - How to return an array from properties of an array of objects 识别数组中具有与另一个对象数组中的属性唯一的属性的对象的最快方法 - quickest way to identify objects in array which have properties that are unique from properties in another object array 基于另一个 object 更新对象属性数组 - Updating an array of objects properties based on another object 从对象数组返回 object - Return object from array of objects 如何在对象的数组中查找属性并将这些对象移动到对象中的另一个数组? - How find properties in an array in object and move these objects to another array in the object? 将属性从一个对象数组添加到另一个对象 - Add properties from one array of objects to another 编写一个纯函数从内部属性返回另一个对象的一个​​对象? - Write a pure function to return one object from inner properties another objects? 如果一个属性等于另一个 object 的属性,如何过滤位于对象数组中的 object 的少数属性 - How to filter few properties of an object which is in Array of objects if one property equals property from another object 创建一个按日期分组并具有count属性的对象数组 - create an array of objects grouped by date with count properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM