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