/array-find

Find array elements. Executes a callback for each element, returns the first element for which its callback returns a truthy value.

Primary LanguageJavaScriptMIT LicenseMIT

array-find

Build Status

Like ES2015 Array.find.

Find array elements. Executes a callback for each element, returns the first element for which its callback returns a truthy value.

Usage

var find = require('array-find');
var numbers = [1, 2, 3, 4];

find(numbers, function (element, index, array) {
  return element === 2;
});
// => 2

var robots = [{name: 'Flexo'}, {name: 'Bender'}, {name: 'Buster'}];

find(robots, function (robot, index, array) {
  return robot.name === 'Bender';
});
// => {name: 'Bender'}

find(robots, function (robot, index, array) {
  return robot.name === 'Fry';
});
// => undefined

Optionally pass in an object as third argument to use as this when executing callback.

Install

$ npm install array-find

License

MIT