I have an array of objects
const data = [
{ category: 'shopping', amount: 50 },
{ category: 'rent', amount: 1000 },
{ category: 'groceries', amount: 20 },
{ category: 'shopping', amount: 50 }
]
and I am trying to sum up the amounts per category
const result = [
{ category: 'shopping', amount: 100 },
{ category: 'rent', amount: 1000 },
{ category: 'groceries', amount: 20 }
]
So far, I'm thinking of removing the 'duplicates' of categories and store them into an array
const temp = data.map((obj) => {
return obj.category
})
const categories = [...new Set(temp)] // ['shopping','rent','groceries']
With the above, i'm thinking of doing a nested loop but after many tries, I have failed.
Any help is appreciated