This method returns the index of the first element in the array that satisfies the provided testing function.
npm install find-index-x href="https://travis-ci.org/Xotic750/find-index-x"
title="Travis status"> src="https://travis-ci.org/Xotic750/find-index-x.svg?branch=master"
alt="Travis status" height="18">
href="https://david-dm.org/Xotic750/find-index-x"
title="Dependency status"> alt="Dependency status" height="18"/>
href="https://david-dm.org/Xotic750/find-index-x?type=dev"
title="devDependency status"> alt="devDependency status" height="18"/>
href="https://badge.fury.io/js/find-index-x"
title="npm version"> alt="npm version" height="18">
href="https://www.jsdelivr.com/package/npm/find-index-x"
title="jsDelivr hits"> alt="jsDelivr hits" height="18">
href="https://bettercodehub.com/results/Xotic750/find-index-x"
title="bettercodehub score"> alt="bettercodehub score" height="18">
href="https://coveralls.io/github/Xotic750/find-index-x?branch=master"
title="Coverage Status"> alt="Coverage Status" height="18">
This method returns the index of the first element in the array that satisfies the provided testing function.
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:
- TypeError If array is null or undefined-
- TypeError If callback is 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
``js
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
``