0

I have an array with objects that have an object property called "operationGroup" with the "groupId" property, like this:

[{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    }
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    }
  }
]

How can I find all the objects with the same groupId and add them to a new array property (groupedOperations) in each object with that groupId? It should not add the array property if the operationGroup is null or if only one groupId is found. The expected output is this:

[{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    },
    groupedOperations: [{
      operation: 22222,
      operationGroup: {
        groupId: 20
      },
      {
        operation: 44444,
        operationGroup: {
          groupId: 20
        }
      }
    }]
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    },
    groupedOperations: [{
        operation: 44444,
        operationGroup: {
          groupId: 20
        }
      },
      {
        operation: 22222,
        operationGroup: {
          groupId: 20
        },
      }
    ]
  }
]
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Gabriel Souza
  • 965
  • 5
  • 16
  • 36
  • why don't you have "groupedOperations" key in your expected output when "groupId" : 1 ? . Is it on purpose or just a mistake – optimalLight Mar 15 '21 at 21:32
  • if the array has only one object with the same groupId then the property groupedOperations should not be added – Gabriel Souza Mar 15 '21 at 21:41
  • 2
    Please [edit] your question to show any research you've done and any attempts you've made based on that research. For instance, there are many questions about grouping arrays, including [Most efficient method to groupby on an array of objects](https://stackoverflow.com/q/14446511/215552) or [Group array items using object](https://stackoverflow.com/q/31688459/215552) – Heretic Monkey Mar 15 '21 at 21:53
  • Try to solve it yourself. Look into `array.reduce` or `array.forEach`. You can ask more specific questions on Stackoverflow, but it's not a forum for solving problems for you. – junvar Mar 15 '21 at 21:56

2 Answers2

1

let reqArray =[{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    }
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    }
  }
]
let groups = {}
let groupReq = []
for (let req of reqArray) {
    if (!req.operationGroup) continue;
    if (!groups[req.operationGroup.groupId]) groups[req.operationGroup.groupId] = [];
    groups[req.operationGroup.groupId].push(req)
}

for(let req of reqArray){
    if(req.operationGroup && groups[req.operationGroup.groupId].length >= 2 ){
        req.groupedOperations = groups[req.operationGroup.groupId]
    }
    groupReq.push(req)
}
console.log(groupReq,groups)

first Filter and group all operation based on groupId. then loop over request data again to update groupedOperations property

Zeeshan Anjum
  • 944
  • 8
  • 16
1
var list = [{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    }
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    }
  }
];

var groupedById =  list.reduce((acc, x) =>  { 
    if(x.operationGroup != null) {
        let groupId = x.operationGroup.groupId; 
        if(!acc[groupId]){
        acc[groupId] = [];
        }
        acc[groupId].push(x); 
    }
    return acc;
}, {});



list.map(x => {
    if(x.operationGroup != null) {
        let groupId = x.operationGroup.groupId;
        if(groupedById[groupId] && groupedById[groupId].length > 1){            
             x["groupedOperations"] = groupedById[groupId];          
        }
    }
});

console.log(list);
optimalLight
  • 194
  • 1
  • 1
  • 13