Skip to content

Instantly share code, notes, and snippets.

@felipem775
Created July 31, 2018 10:59
Show Gist options
  • Save felipem775/70394357462a393d7239a6ddc526ef6c to your computer and use it in GitHub Desktop.
Save felipem775/70394357462a393d7239a6ddc526ef6c to your computer and use it in GitHub Desktop.
Find the object whose property "X" has the greatest value in an array of objects
// https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects Andy Polhill
var n1 = { dateTime: new Date(), name: "n1"}
var n2 = { dateTime: new Date(), name: "n2"}
data = [n1, n2]
data.reduce(function (prev, current) { return (prev.dateTime > current.dateTime) ? prev : current })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment