0

I have players containing multiple objects within. My end goal is to return the name property of the object with the highest points value.

For example this block should return a string of Bob

const players = [

   { 
      name: 'Jack',
      points: 3
   }

   { 
      name: 'Jill',
      points: 2
   }

   { 
      name: 'Bob',
      points: 4
   }

]


for(i = 0; i < players.length; i++){

   // return string of name property of object with highest value in points

}

I'm not sure how to execute this inside the for loop

Elitezen
  • 6,551
  • 6
  • 15
  • 29

1 Answers1

1

You can reduce it

players.reduce((acc, obj)=>{
   return (acc.points || 0)<obj.points ? obj : acc;
},{})
gorak
  • 5,233
  • 1
  • 7
  • 19