I am dealing with an array containing objects that have a specific property called "operationGroup" with the sub-property "groupId". Here is an example of the array structure:
[{
operation: 11111,
operationGroup: null
},
{
operation: 22222,
operationGroup: {
groupId: 20
}
},
{
operation: 33333,
operationGroup: {
groupId: 1
}
},
{
operation: 44444,
operationGroup: {
groupId: 20
}
}
]
I need to identify all objects sharing the same groupId and then group them together in a new array property (groupedOperations) within each object. However, this should not be done if the operationGroup is null or if only one groupId is present. The desired output can be seen below:
[{
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
},
}
]
}
]