_.findIndex

Returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 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. var index = _.findIndex(users, function (o) { return o.age >= 40; })
  9. console.log(index)
  10. // output: 1
  11.  
  12. // Native
  13. var users = [
  14. { 'user': 'barney', 'age': 36, 'active': true },
  15. { 'user': 'fred', 'age': 40, 'active': false },
  16. { 'user': 'pebbles', 'age': 1, 'active': true }
  17. ]
  18.  
  19. var index = users.findIndex(function (o) { return o.age >= 40; })
  20. console.log(index)
  21. // output: 1

Browser Support for Array.prototype.findIndex()

ChromeEdgeFirefoxIEOperaSafari
45.0 ✔25.0 ✔32.0 ✔7.1 ✔