how to add 2 properties of 2 different objects in js (nodejs)

0

I have these 3 objects

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 }
]

and I have to know the total of wins per league, that is to add the wins of object 3 that are related to object 1 through object 2.

I have occupied functions like .find

(wins: (winsByTeams.find(win => win.teamId === team.id)).wins)

To find ids that are the same, but I wonder if there is any special function to add the wins of the 3 object without transforming them into an array through .map

( const ligasOrdenadas = leagues.map((leagues) => ({
        name: leagues.name,
        winsTotal: (winsByTeams.find(win => win.teamId === leagues.id)).wins        

}));)
    
asked by Cristian Andres Godoy Rodrigue 28.12.2018 в 08:36
source

1 answer

3

As always, everything is to apply divide and conquer :

  • Given a league, you filter the IDs of the teams that belong to that league:

    const teamIds = teamsByLeague
         .filter(team => team.leagueId ===leagueId) //nos quedamos los equipos de la liga
         .map(team => team.teamId); // y cogemos sus IDs
    
  • Once we have the teams, we add their victories:

    return winsByTeams
         .reduce((acc,tw) => teamIds.includes(tw.teamId) ? acc + tw.wins: acc, 0);
    

    That could also be written as:

    return winsByTeams
         .filter(tw => teamIds.includes(tw.teamId))
         .reduce((acumulador,team) => acumulador + team.wins, 0);
    

And everything together is like this:

function getWinsByLeague(leagueId) {

  const teamIds = teamsByLeague
    .filter(team => team.leagueId ===leagueId) //nos quedamos los equipos de la liga
    .map(team => team.teamId); // y cogemos sus IDs
  console.log('Equipos de la liga',leagueId,':',teamIds.toString());
  
  // sumamos las victorias de los equipos si su ID está en la lista obtenida
  return winsByTeams
    .filter(tw => teamIds.includes(tw.teamId))
    .reduce((acumulador,team) => acumulador + team.wins, 0);
}

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 }
]



leagues.forEach(l => console.log('La liga',l.name,'tiene',getWinsByLeague(l.id),'victorias'));
    
answered by 28.12.2018 / 09:58
source