'Compare the object fields within array

Compare the age of the objects in an array and keep only the one with the highest age. I've gone through the solution which uses reduce function, but is there any other optimal way other than using reduce? I am new to javascript, and still learning the concepts.

array: [ { "name": "sample", "age": 22 },{ "name": "sample2", "age": 42 }]


Solution 1:[1]

You can achieve this with sort:

const array = [ { "name": "sample", "age": 22 },{ "name": "sample2", "age": 42 }]

const max = array.sort((a, b) => {
  return b.age - a.age
})[0]

console.log([max])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 symlink