In my current project, I am working with an array of objects. Each object in this array contains both an amount and a value property. What I need to achieve is that if two or more objects have the same amount value, I want to combine their values into one object.
For clarity, here is an example array:
const array = [
{
"key": 1,
"amount": 11,
"value": "were"
},
{
"key": 2,
"amount": 6,
"value": "locomotives"
},
{
"key": 3,
"amount": 5,
"value": "They"
},
{
"key": 4,
"amount": 5,
"value": "with"
},
{
"key": 5,
"amount": 4,
"value": "used"
}
]
The desired transformation would look like this:
const array = [
{
"key": 1,
"amount": 11,
"value": "were"
},
{
"key": 2,
"amount": 6,
"value": "locomotives"
},
{
"key": 3,
"amount": 5,
"value": "They, width"
},
{
"key": 5,
"amount": 4,
"value": "used"
}
]
I have attempted using methods like reduce
and map
, but I have encountered difficulties in properly joining the values together.