This is what I have:
const arrayA = [{name:'a', amount: 10, serviceId: '23a', test:'SUCCESS'},
{name:'a', amount: 9, test:'FAIL'},
{name:'b', amount: 15, serviceId: '23b', test:'SUCCESS'}]
(note that there's object not having 'serviceId')
I would like to get:
[{name:'a', amount: 19, test:'FAIL'},
{name:'b', amount: 15, test:'SUCCESS'}]
- I want to sum
amountfield grouped by name. - 'test' field should be
FAILif there's any object with the valueFAIL(grouped by name) - I don't care about the value of
serviceIdand don't want to include it in the new object.
I have searched, and tried something like this: (reference: https://stackoverflow.com/a/50338360/13840216)
const result = Object.values(arrayA.reduce((r, o) => (r[o.name]
? (r[o.name].amount += o.amount)
: (r[o.name] = {...o}), r), {}));
but still not sure how to assign test field.
Any help would be appreciated!