
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
finds an item in an array matching a predicate function, and returns its 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.
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
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.
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.
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.
npm install find-index
var findIndex = require('find-index/findIndex')
var findIndex = require('find-index/ponyfill') // will use native Array#findIndex if available.
var findLastIndex = require('find-index/findLastIndex') // search backwards from end
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
$ 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
[1.1.1] - 2019-02-12
FAQs
finds an item in an array matching a predicate function, and returns its index
The npm package find-index receives a total of 605,468 weekly downloads. As such, find-index popularity was classified as popular.
We found that find-index demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.