I have a main array –
const arr = [
{ description: 'Senior', amount: 50 },
{ description: 'Senior', amount: 50 },
{ description: 'Adult', amount: 75 },
{ description: 'Adult', amount: 35 },
{ description: 'Infant', amount: 25 },
{ description: 'Senior', amount: 150 }
]
I want help with an es6 operation which will add the amount based on the key(description) and remove the duplicates.
Result array will somewhat look like –
const newArr = [
{ description: 'Senior', amount: 120 },
{ description: 'Adult', amount: 110 },
{ description: 'Infant', amount: 25 },
{ description: 'Senior', amount: 150 }
]
I have been using the reduce operator to achieve this using the solution, but that removes the non-consecutive objects as well.
It would be really helpful if someone can help me with some es6 operators to perform the same operation.