My goal is to merge the array itself and transform it into a more meaningful array
array = [
{item: 'pen', madeIn: 'US', color: 'blue'},
{item: 'pen', madeIn: 'US', color: 'white'},
{item: 'pen', madeIn: 'China', color: 'red'},
{item: 'pen', madeIn: 'China', color: 'white'}
]
The desired output array I am trying to create :
outputArray = [
{item: 'pen', madeIn: 'US', color: ['blue', 'white']},
{item: 'pen', madeIn: 'China', color: ['red', 'white']}
];
I have attempted different methods without success. One solution involves using a temporary variable to store item and madeIn values, followed by another loop to compare items and madeIn values and then add colors to an array. Multiple loops are needed to solve this issue.
While this method works, it is not the most efficient solution. Any other ideas or suggestions would be greatly appreciated. Thank you.