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 }]
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])