1

I have a JS array of objects

const a = [
  {name: karl, age: 53}, 
  {name: fred, age: 34}, 
  {name: annie, age: 12},
  // ...
];

and I would like to extract the objects with the maximum age. I could perhaps sort the entire array and just pick the first element, but that seems somewhat wasteful.

1
  • 1
    getting max age from a collection example underscorejs.org/#max Commented Jan 15, 2016 at 12:24

1 Answer 1

4

You can simply use _.max, like this

console.log(_.max(a, 'age'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.