ES6 Find the maximum number of an array of objects

前端 未结 7 1291
栀梦
栀梦 2021-01-14 17:27

I have the following data

shots = [
    {id: 1, amount: 2},
    {id: 2, amount: 4}
]

Now I\'m trying to get the object which

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 17:51

    Try this:

    Updated

    let highest = shots.reduce((max, shot) => {
      return shot.amount > max.amount ? shot : max
    }, {amount:0});
    

提交回复
热议问题