Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
Updating an array of objects properties based on another object

I want to update an arrays of objects (league table) based on another array of object (match result) by finding id from the two teams and update the stats on the league table,

Just like how football(soccer) league table works.

This is how the league standing looks

let leagueStandings = [
  {id:'49e93e0d', played: 0, scored: 0, conceded: 0, won: 0, drawn: 0, lost: 0},
  {id:'24e5ddb8', played: 0, scored: 0, conceded: 0, won: 0, drawn: 0, lost: 0}
]

I get this match result array of objects for both teams, which I need to update on the league standings..

let matchResult = [
  { id: '49e93e0d', scored: 2, conceded: 1, win: true, draw: false },
  { id: '24e5ddb8', scored: 1, conceded: 2, win: false, draw: false }
]

So I came up with this code

function updateStandings(match) {
    let team;
    match.forEach(prop => { //loop both teams in result and update their respective stats
        team = leagueStandings.find(team => team.id === prop.id); // find the team to update by id
        team.played++
        team.scored += prop.scored
        team.conceded += prop.conceded
        team.goalDifference += (prop.scored - prop.conceded)
        if (prop.win) team.won++
        if (prop.draw) team.drawn++
        if (!prop.win && !prop.draw) team.lost++
    });
}

updateStandings(matchResult)

// outputs the expected the result
[
  { id: '49e93e0d', played: 1, scored: 2, conceded: 1, won: 1, drawn: 0, lost: 0 },
  { id: '24e5ddb8', played: 1, scored: 1, conceded: 2, won: 0, drawn: 0, lost: 1 }
]

Which actually works and does the job, however I think there is better way to do it? also the league standings array will contain large nums of teams, so I'm not sure if it's the best way?

expected output

[
  { id: '49e93e0d', played: 1, scored: 2, conceded: 1, won: 1, drawn: 0, lost: 0 },
  { id: '24e5ddb8', played: 1, scored: 1, conceded: 2, won: 0, drawn: 0, lost: 1 }
]

Full code HERE

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The find() function in your solution will go through every leagueStandings item to match the id

Because leagueStandings is unordered on average it has to loop though half the items before it finds a match.

This can be fixed by indexing by ID

consider using this structure for leagueStandings:

let leagueStandings = {
  '49e93e0d': {played: 0, scored: 0, conceded: 0, won: 0, drawn: 0, lost: 0},
  '24e5ddb8': {played: 0, scored: 0, conceded: 0, won: 0, drawn: 0, lost: 0}
}

Now you can change this line:

team = leagueStandings.find(team => team.id === prop.id);

to this:

team = leagueStandings[prop.id];

and do an indexed lookup

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda