Monday, 28 October 2019

Calculate the greater value between objects in different arrays

I have a score array containing two objects: Liga and Premier. These 2 objects are an array of a list of teams.

I was able to define the greater string when score was previously a single array of objects.

This is the demo i have reproduced where the comparison works fine.

This is the code calculating the higher value comparing the 2 objects.

const maxAverage = teams => {
  return teams.map(team => {
      return {
          team:team, 
          avg: getAverage(team)
      }
  }).reduce((a,b)=>a.avg>b.avg?a:b).team
}


<p>Stronger Team:{maxAverage([this.state.homeCity,this.state.awayCity])</p>

The problem now is that now score is an array of the 2 object as i said and i am trying to change my function in something like

const maxAverage = (league, teams) => {
  return teams.map(team => {
      return {
          team:team,
          avg: getAverage(league,team)
      }
  }).reduce((a,b)=>a.avg>b.avg?a:b).team
}

I am not able to pass to my function maxAverage the parameter of one of the two leagues selected and then the 2 objects ( teams ) i want to compare.

i want to do something like this:

<p>Stronger Team:{maxAverage([this.state.selectedLeague], this.state.selectedHomeTeam,this.state.selectedAwayTeam])}

This is the other demo i have reproduced with the current situation.



from Calculate the greater value between objects in different arrays

No comments:

Post a Comment