I want to add sum of first, second and third place to all teams after filtering the basic data.
But I am stuck with data which I want to push and I don't know how to fix this. Every time when function find a team with first position on specific season I want to add this fact to table medalTables to specific team
f.e in season 2012 Barca was first and I want to add this to
{team: Barca, first: 1} etc.
(data are only example)
Code:
let medalTeams = [{
name: "Real",
first: 0,
second: 0,
third: 0
},{
name: "Barca",
first: 0,
second: 0,
third: 0
},{
name: "Atletico",
first: 0,
second: 0,
third: 0
}
]
const medals = tables
.filter((team) => (team.position === 1 || team.position === 2 || team.position === 3))
.map((team, index) => {
if(team.position === 1)
medalTeams.filter(team => team.name === team.club).push({first: 1})
});
What I want to reach:
/*
let medalTeams = [{
name: "Real",
first: 3,
second: 1,
third: 0
},{
name: "Barca",
first: 1,
second: 2,
third: 1
},{
name: "Atletico",
first: 0,
second: 1,
third: 3
}
]
*/
Basic data:
const tables = [
{
season: 2011,
position: 1,
club: 'Real'
},
{
season: 2011,
position: 2,
club: 'Barca'
},
{
season: 2011,
position: 3,
club: 'Atletico'
},
{
season: 2012,
position: 2,
club: 'Real'
},
{
season: 2012,
position: 1,
club: 'Barca'
},
{
season: 2012,
position: 3,
club: 'Atletico'
},
{
season: 2013,
position: 1,
club: 'Real'
},
{
season: 2013,
position: 3,
club: 'Barca'
},
{
season: 2013,
position: 2,
club: 'Atletico'
},
{
season: 2014,
position: 1,
club: 'Real'
},
{
season: 2014,
position: 2,
club: 'Barca'
},
{
season: 2014,
position: 3,
club: 'Atletico'
},
]