【问题标题】:Return Object Name With The Biggest Points Property In a List Of Objects [duplicate]返回对象列表中具有最大点属性的对象名称[重复]
【发布时间】:2020-05-29 17:39:17
【问题描述】:

我有 players 包含多个对象。我的最终目标是返回具有最高 points 值的对象的 name 属性。

例如这个块应该返回一个字符串 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

}

我不确定如何在 for 循环中执行此操作

【问题讨论】:

    标签: javascript arrays object properties max


    【解决方案1】:

    你可以reduce它

    players.reduce((acc, obj)=>{
       return (acc.points || 0)<obj.points ? obj : acc;
    },{})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 2010-11-09
      • 2017-11-24
      • 2015-06-14
      • 1970-01-01
      • 2013-07-23
      相关资源
      最近更新 更多