How to create an object where the keys are the names of another object

-2

if we have the objects

const leagues = [
  { id: 1, country: 'England', name: 'Premier League' },
  { id: 2, country: 'Germany', name: 'Bundesliga' },      
  { id: 3, country: 'France', name: 'Lige 1' }
]    

const teams = [
  { id: 1, country: 'Spain', name: 'Real Madrid C.F.' },
  { id: 2, country: 'Italy', name: 'A.C. Milan' },
  { id: 3, country: 'France', name: 'Olympique Marseille' }

]

And I want to create

const leaguesPeorEquipo = leagues.map((ligas) => ({
    name: ligas.name,
    country: ligas.country
}));

but instead of name: as Key I would like to put team.name since there is the name of the team I want as Key and then assign the values a name

    
asked by Cristian Andres Godoy Rodrigue 28.12.2018 в 17:06
source

1 answer

0
function leaguesWithTeamWithLestWins() {
    let object = {};

    leagues.forEach(league => {

        const teamIds = teamsByLeague
            .filter(team => team.leagueId === league.id) //nos quedamos los equipos de la liga
            .map(team => team.teamId); // y cogemos sus IDs

        const wins = winsByTeams
            .filter(tw => teamIds.includes(tw.teamId))
            .reduce(function(prev, curr) {
                return prev.wins < curr.wins ? prev : curr;
            });

        object[league.name] = teams.find(team => team.id == wins.teamId).name;

    });

    return object;
}

thanks Pablo Lozano

    
answered by 28.12.2018 в 20:15