0

I have an array of objects that contains hockey matches and I need to get through them to create scoreboard in another array of objects.

I give you example.

I'd have array for example like this:

let array = [
           {team1:"Toronto", team2:"Oilers", score1:5, skore:0},
           {team1:"Toronto", team2:"Rangers", score1:3, skore:1},
           {team1:"Toronto", team2:"Penguins", score1:0, skore:1},
           {team1:"Oilers", team2:"Rangers", score1:2, skore:2},
           {team1:"Oilers", team2:"Penguins", score1:2, skore:3},
           {team1:"Rangers", team2:"Penguins", score1:0, skore:0}
           ]

And I'd need a function that would get me array like this:

let array2 = [
  {Team:"Toronto", Wins:2, Draws:0, Losses:1, ShotGoals:8, RecievedGoals:2},
  {Team:"Oilers", Wins:0, Draws:1, Losses:2, ShotGoals:4, RecievedGoals:10},
  {Team:"Penguins", Wins:1, Draws:1, Losses:2, ShotGoals:2, RecievedGoals:4},
  {Team:"Rangers", Wins:1, Draws:2, Losses:0, ShotGoals:3, RecievedGoals:2},
]

I tried to loop through the array with forEach and push new or change existing teams in the array2 but can't get it working as the array2, that I'm creating is changing, as the loop goes. It also needs to be dynamic so it still works if the first array is extended etc. and I'm just not able to make it work right now.

I'm assuming I will have to use some array methods but I can't seems to figure out which one and how as I am quite new to this.

Any help is highly appreciated. Thank you.

3
  • Do some research into doing "groupBy" using the team names as your groupBy object properties. Then you will need some logic to compare the 2 scores to add up wins/losses and draws Commented May 18, 2020 at 1:08
  • Thanks for the comment. I've doen little googling and that looks exactly like what I'm looking for. Although hard to say how implementic the logic will go but I'll give it a try. Cheers. Commented May 18, 2020 at 1:12
  • I made my attempt Commented May 18, 2020 at 1:20

1 Answer 1

1
let array = [
           {team1:"Toronto", team2:"Oilers", score1:5, skore:0},
           {team1:"Toronto", team2:"Rangers", score1:3, skore:1},
           {team1:"Toronto", team2:"Penguins", score1:0, skore:1},
           {team1:"Oilers", team2:"Rangers", score1:2, skore:2},
           {team1:"Oilers", team2:"Penguins", score1:2, skore:3},
           {team1:"Rangers", team2:"Penguins", score1:0, skore:0}
           ]


let result = {};

let addToTeam = (t,w,d,l)=>{
if(result[t] == null)
return result[t] = {team:t,wins:w,draws:d,losses:l}
    result[t].wins+=w
    result[t].draws+=d
    result[t].losses+=l
}

array.forEach(m => {
if(m.score1>m.skore){
    addToTeam(m.team1,1,0,0)
    addToTeam(m.team2,0,0,1)
}else if(m.score1<m.skore){
    addToTeam(m.team1,0,0,1)
    addToTeam(m.team2,1,0,0)
}else{
    addToTeam(m.team1,0,1,0)
    addToTeam(m.team2,0,1,0)
}})

result = Object.values(result)

console.log(result);

results in

[{"team":"Toronto","wins":2,"draws":0,"losses":1},   
{"team":"Oilers","wins":0,"draws":1,"losses":2},    
{"team":"Rangers","wins":0,"draws":2,"losses":1},    
{"team":"Penguins","wins":2,"draws":1,"losses":0}]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I've just put in the shot goals and recieved goals to add same way the other things do and it works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.