I have an array of objects with nested arrays inside, and I need to restructure it according to my API requirements.
[{
containerId: 'c12',
containerNumber: '4321dkjkfdj',
goods: [{
weight: '32kg',
quantity: '3'
}]
},
{ containerId: 'c12', containerNumber: '4321dkjkfdj', goods: [{
weight: '322kg',
quantity: '32'
}]
},
{
containerId: 'c13',
containerNumber: '1212dkjkfdj',
goods: [{
weight: '13kg',
quantity: '3'
}]
},
{containerId: 'c13', containerNumber: '1212dkjkfdj', goods: [{
weight: '13kg',
quantity: '3'
}]
},
]
I need to consolidate objects with the same 'containerId' into one object, including all 'goods' under that 'containerId' as shown in the code below:
[{
containerId: 'c12',
containerNumber: '4321dkjkfdj',
goods: [{
weight: '32kg',
quantity: '3'
},
{
weight: '322kg',
quantity: '32'
}
]
},
{
containerId: 'c13',
containerNumber: '1212dkjkfdj',
goods: [{
weight: '13kg',
quantity: '3'
},
{
weight: '13kg',
quantity: '3'
}
]
}
]