Hello I'm currently struggling with this and I don't really know what to do.
I have array of objects like this:
[
{
rating: "good",
count: 4,
},
{
rating: "bad",
count: 2,
},
{
rating: "bad",
count: 4,
}
]
And what I'm trying to get is:
[
{
rating: "good",
count: 4,
total: 4
},
{
rating: "bad",
count: 2,
total: 6
},
{
rating: "bad",
count: 4,
total: 6
}
]
I know that I need to use array reduce but I don't know how to write code to make a structure of array like this.
reduceis fine if you're doing functional programming with predefined, reusable, tested reducer functions. If you aren't, it's just an overcomplicated loop -- hard to read, easy to get wrong. You need a loop (probably two -- one to do the totals, probably storing them in aMap, and one to put the totals on all the objects once you have them).