How to do reduce math.max to an array of objects

  • A+
Category:Languages

I have an array of objects. I want to get the max out of the number attribute from the array:

[   {number: 1000, name: "Josh"},    {number: 2000, name: "Joker"},    {number: 3000, name: "Batman"} ] 

I'm using this solution but I keep getting NAN:

const max = arr.reduce((a, b) => Math.max(a.number, b.number)); 

My goal is to get the max and then store it in a variable

const x = { number: 3000, name: "Batman"} 

How would I achieve it with reduce? It seems like it is only working with array of numbers.

 


Done by simple reduce,

var arr=[{"number":1000,"name":"Josh"},{"number":2000,"name":"Joker"},{"number":3000,"name":"Batman"}]     var x = arr.reduce((acc, i)=>(i.number > acc.number ? i : acc))  console.log(x)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: