Licence
MIT
Version
3.1.2
Deps
5
Size
268 kB
Vulns
0
Weekly
0
find-index-x
This method returns the index of the first element in the array that satisfies the provided testing function.
module.exports ⇒ number
Like findIndex, this method returns an index in the array, if an element
in the array satisfies the provided testing function. Otherwise -1 is returned.
Kind: Exported member
Returns: number - Returns index of positively tested element, otherwise -1.
Throws:
TypeErrorIf array isnullorundefined-TypeErrorIfcallbackis not a function.
| Param | Type | Description |
|---|---|---|
| array | Array |
The array to search. |
| callback | function |
Function to execute on each value in the array, taking three arguments: element, index and array. |
| [thisArg] | * |
Object to use as this when executing callback. |
Example
import findIndex from 'find-index-x';
function isPrime(element, index, array) {
let start = 2;
while (start <= Math.sqrt(element)) {
if (element % start < 1) {
return false;
}
start += 1;
}
return element > 1;
}
console.log(findIndex([4, 6, 8, 12, 14], isPrime)); // -1, not found
console.log(findIndex([4, 6, 7, 12, 13], isPrime)); // 2