Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda