Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!