What is find-index?
The find-index npm package is a utility that helps you find the index of the first element in an array that satisfies a provided testing function. It is useful for quickly locating elements in arrays without having to write custom loops.
What are find-index's main functionalities?
Find index of first matching element
This feature allows you to find the index of the first element in an array that satisfies the provided testing function. In this example, the function checks for elements greater than 13, and the first such element is at index 3.
const findIndex = require('find-index');
const array = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
const index = findIndex(array, isLargeNumber);
console.log(index); // Output: 3
Find index with custom predicate
This feature allows you to use a custom predicate function to find the index of the first element that matches the condition. In this example, the predicate checks for a user with the name 'Jane', which is found at index 1.
const findIndex = require('find-index');
const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Jack' }
];
const predicate = (user) => user.name === 'Jane';
const index = findIndex(users, predicate);
console.log(index); // Output: 1
Other packages similar to find-index
lodash
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including array manipulation. The `_.findIndex` method in Lodash offers similar functionality to the find-index package, allowing you to find the index of the first element in an array that matches a given predicate.
array-find-index
The array-find-index package is another utility that provides a similar function to find-index. It is a lightweight package that offers a method to find the index of the first element in an array that satisfies a provided testing function. It is similar in scope and usage to find-index.
find-index
An implementation of the ES6 method Array.prototype.findIndex
as a standalone module and a
ponyfill.
Finds an item in an array matching a predicate function, and returns its index.
Fast both when thisArg
is used and also when it isn't.
usage
npm install find-index
var findIndex = require('find-index/findIndex')
var findIndex = require('find-index/ponyfill')
var findLastIndex = require('find-index/findLastIndex')
findIndex(array, callback[, thisArg])
findLastIndex(array, callback[, thisArg])
Parameters:
array
The array to operate on.
callback
Function to execute on each value in the array, taking three arguments:
element
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array findIndex was called upon.
thisArg
Object to use as this when executing callback.
Based on array-findindex
performance
$ iojs --harmony_arrays perf/benchmark.js
native Array.prototype.findIndex: 6347ms
findIndex: 1633ms
findIndex ponyfill: 6384ms
findLastIndex: 1508ms
npm lodash.findindex: 2900ms
npm array-findindex: 3512ms