Tengo estos 3 objetos
const leagues = [
{ id: 1, country: 'England', name: 'Premier League' },
{ id: 2, country: 'Germany', name: 'Bundesliga' },
{ id: 3, country: 'Netherlands', name: 'Eredivisie' },
{ id: 4, country: 'Spain', name: 'La Liga' },
{ id: 5, country: 'Italy', name: 'Serie A' },
{ id: 6, country: 'Portugal', name: 'Liga NOS' },
{ id: 7, country: 'France', name: 'Lige 1' }
]
const teamsByLeague = [
{ teamId: 12, leagueId: 5 },
{ teamId: 6, leagueId: 3 },
{ teamId: 2, leagueId: 5 },
{ teamId: 3, leagueId: 4 },
{ teamId: 4, leagueId: 2 },
{ teamId: 8, leagueId: 1 },
{ teamId: 10, leagueId: 6 },
{ teamId: 5, leagueId: 1 },
{ teamId: 7, leagueId: 5 },
{ teamId: 9, leagueId: 1 },
{ teamId: 11, leagueId: 2 },
{ teamId: 1, leagueId: 4 },
{ teamId: 13, leagueId: 7 }
]
const winsByTeams = [
{ teamId: 10, wins: 2 },
{ teamId: 6, wins: 4 },
{ teamId: 5, wins: 5 },
{ teamId: 1, wins: 13 },
{ teamId: 7, wins: 3 },
{ teamId: 4, wins: 5 },
{ teamId: 8, wins: 3 },
{ teamId: 2, wins: 7 },
{ teamId: 9, wins: 1 },
{ teamId: 3, wins: 5 },
{ teamId: 11, wins: 1 },
{ teamId: 12, wins: 2 },
{ teamId: 13, wins: 1 }
]
y tengo que saber el total de wins por league, osea sumar los wins del objeto 3 que están relacionados al objeto 1 a través del objeto 2.
He ocupado funciones como .find
(wins: (winsByTeams.find(win => win.teamId === team.id)).wins)
Para encontrar las id que sean igual, pero me pregunto si hay alguna funcion especial para sumar los wins del 3 objeto sin transformarlos en arreglo a través de .map
( const ligasOrdenadas = leagues.map((leagues) => ({
name: leagues.name,
winsTotal: (winsByTeams.find(win => win.teamId === leagues.id)).wins
}));)