I'm trying to learn better use of JS functions and I have a feeling what I'm after can be done using reduce or map or both, but I simply cannot find examples of this particular scenario:
I have an array of objects, eg:
const data = [
{
"member": "111111",
"offers": 0,
"categories": 0,
"total": 100,
"favorites": 0
},
{
"member": "111111",
"offers": 2,
"categories": 1,
"total": 400,
"favorites": 1
},
{
"member": "222222",
"offers": 1,
"categories": 0,
"total": 50,
"favorites": 5
}
]
The goal is to reduce to an array that merges all the key values into ONE array for each member, ie:
{
"member": "111111",
"offers": 2,
"categories": 1,
"total": 500,
"favorites": 1
},
{
"member": "222222",
"offers": 1,
"categories": 0,
"total": 50,
"favorites": 5
},
Therefore, there were two entries for member 111111, they have been merged and all the other values summed into a single array.
Honestly, everything I've tried is a mess, so apart from using the boring for loops or adding code here that is clearly rubbish, I hope you can point me in the right direction.