Nico Schlömer
Nico Schlömer

Reputation: 58831

Extract array element with maximum property value

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.

Upvotes: 1

Views: 45

Answers (1)

thefourtheye
thefourtheye

Reputation: 239573

You can simply use _.max, like this

console.log(_.max(a, 'age'));

Upvotes: 4

Related Questions