What is array-find?
The array-find npm package provides a simple utility to find the first element in an array that satisfies a provided testing function. It is a lightweight alternative to the native Array.prototype.find method, offering similar functionality.
What are array-find's main functionalities?
Finding an element in an array
This feature allows you to find the first element in an array that satisfies a given condition. In this example, the condition is that the element should be greater than 3.
const find = require('array-find');
const array = [1, 2, 3, 4, 5];
const found = find(array, function(element) {
return element > 3;
});
console.log(found); // Output: 4
Other packages similar to array-find
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It includes a `find` method that works similarly to array-find but offers a broader range of utility functions for working with arrays, objects, and other data types.
underscore
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes a `find` method that is similar to array-find but is part of a larger suite of utility functions.
ramda
Ramda is a practical functional library for JavaScript programmers. It makes it easy to create functional pipelines and includes a `find` function that works similarly to array-find but is designed with a functional programming approach in mind.
array-find
ES 2015 Array.find
ponyfill.
Ponyfill: A polyfill that doesn't overwrite the native method.
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;
});
var robots = [{name: 'Flexo'}, {name: 'Bender'}, {name: 'Buster'}];
find(robots, function (robot, index, array) {
return robot.name === 'Bender';
});
find(robots, function (robot, index, array) {
return robot.name === 'Fry';
});
Optionally pass in an object as third argument to use as this
when executing callback.
Install
$ npm install array-find
License
MIT