_.find

Returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

  1. // Underscore/Lodash
  2. var users = [
  3. { 'user': 'barney', 'age': 36, 'active': true },
  4. { 'user': 'fred', 'age': 40, 'active': false },
  5. { 'user': 'pebbles', 'age': 1, 'active': true }
  6. ]
  7.  
  8. _.find(users, function (o) { return o.age < 40; })
  9. // output: object for 'barney'
  10.  
  11. // Native
  12. var users = [
  13. { 'user': 'barney', 'age': 36, 'active': true },
  14. { 'user': 'fred', 'age': 40, 'active': false },
  15. { 'user': 'pebbles', 'age': 1, 'active': true }
  16. ]
  17.  
  18. users.find(function (o) { return o.age < 40; })
  19. // output: object for 'barney'

Browser Support for Array.prototype.find()

ChromeEdgeFirefoxIEOperaSafari
45.0 ✔25.0 ✔32.0 ✔7.1 ✔