I have an matchesScoreResult array of objects like this:
[
{
roundId: '397a57f6-c9da-4017-bf98-62d7d48c1da5',
teamId: '32305c41-00e8-492a-859c-83c262230e06',
score: '7'
},
{
roundId: '397a57f6-c9da-4017-bf98-62d7d48c1da5',
teamId: '1122ef35-8bce-4310-838b-8221228cadc9',
score: '18'
},
{
roundId: 'c91f1a16-df97-4716-bb0d-8589612da704',
teamId: '32305c41-00e8-492a-859c-83c262230e06',
score: '21'
},
{
roundId: 'c91f1a16-df97-4716-bb0d-8589612da704',
teamId: '1122ef35-8bce-4310-838b-8221228cadc9',
score: '19'
}
]
This is an array for some rounds in a game (as you can see, the roundId key is found twice the same, because there are two teams that play in the same round, and the case is that the same two teams played two different rounds)
Based on the roundId and the team that won I want to increment firstTeamRoundsWon variable or secondTeamRoundsWon.
First I get a unique round ids array like this:
let uniqueRoundIds = [...new Set(matchesScoreResult.map(item => item.roundId))]
Based on that uniqueRoundIds array I do the following operations:
uniqueRoundIds.map(roundId => matchesScoreResult.filter(teamRound => teamRound.roundId === roundId)
.map(round => round.reduce((previousValue, currentValue) => previousValue.score > currentValue.score ? firstTeamRoundsWon++ : secondTeamRoundsWon++))
My problem is that it increments twice the firstTeamRoundsWon but based on my data, both variables should be 1.
Is there something wrong that I did there?
I am open to other ways of resolving this.
Thank you for your help!