Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xotic750/find-index-x
Return the index of the first element in the array that satisfies the provided testing function.
https://github.com/xotic750/find-index-x
array browser ecmascript findindex nodejs
Last synced: 2 months ago
JSON representation
Return the index of the first element in the array that satisfies the provided testing function.
- Host: GitHub
- URL: https://github.com/xotic750/find-index-x
- Owner: Xotic750
- License: mit
- Created: 2017-03-30T08:21:21.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:54:50.000Z (about 2 years ago)
- Last Synced: 2024-11-11T20:48:03.345Z (2 months ago)
- Topics: array, browser, ecmascript, findindex, nodejs
- Language: JavaScript
- Homepage:
- Size: 3.44 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## 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**:-
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
```