I have an array of objects (responses), which can contain any number of objects (i.e. indeterminate number of rounds and questions), in any order. The objects inside will be similar:
responses [
{
name: "personA"
uniqueId: "abcd"
roundNumber: 0
questionNumber: 0
score: 1
},
{
name: "personA"
uniqueId: "abcd"
roundNumber: 0
questionNumber: 1
score: 1
},
{
name: "personA"
uniqueId: "abcd"
roundNumber: 1
questionNumber: 0
score: 0
},
name: "personB"
uniqueId: "efgh"
roundNumber: 0
questionNumber: 0
score: 1
},
{
name: "personB"
uniqueId: "efgh"
roundNumber: 0
questionNumber: 1
score: 0
},
{
name: "personB"
uniqueId: "efgh"
roundNumber: 1
questionNumber: 0
score: 1
}
]
How would I group and sum the scores depending on the values of the objects? More specifically I would like to take all the objects for personA (using their name and uniqueId fields), then sum up the scores by roundNumber. Then repeat the process for every player.
Example of flow:
- Sum the score of
personAforroundNumber: 0, and save that. - Sum the score of
personAforroundNumber: 1, and save that, etc... - Repeat for
personB, thenpersonC, etc...
The resulting array could be something like:
sorted = [
{
name: "personA",
uniqueId: "abcd",
scores: [2, 0]
},
{
name: "personB",
uniqueId: "efgh",
scores: [1, 1]
}
]
I've looked at for loops, indexOf, map, filter, reduce, and I'm struggling hard with this one because the amount of players, rounds, or questions could be any amount.