index-of-x
v3.1.2
Published
An extended ES6 indexOf.
Downloads
2,300
Maintainers
Readme
index-of-x
An extended ES6 indexOf.
Version: 2.3.0
Author: Xotic750 Xotic750@gmail.com
License: MIT
Copyright: Xotic750
module.exports(array, searchElement, [fromIndex], [extend])
⇒ number ⏏
This method returns the first index at which a given element can be found in the array, or -1 if it is not present.
Kind: Exported function
Returns: number - Returns index of found element, otherwise -1.
Throws:
- TypeError If
array
isnull
orundefined
.
| Param | Type | Description |
| ------------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| array | Array | The array to search. |
| searchElement | * | Element to locate in the array
. |
| [fromIndex] | number | The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched. Default: 0 (entire array is searched). |
| [extend] | string | Extension type: SameValue
or SameValueZero
. |
Example
import indexOf from 'index-of-x';
const subject = [2, 3, undefined, true, 'hej', null, 2, false, 0, -0, NaN];
// Standard mode, operates just like `Array.prototype.indexOf`.
console.log(indexOf(subject, null)); // 5
console.log(indexOf(subject, '2')); // -1
console.log(indexOf(subject, NaN)); // -1
console.log(indexOf(subject, -0)); // 8
console.log(indexOf(subject, 2, 2)); //6
// `SameValueZero` mode extends `indexOf` to match `NaN`.
console.log(indexOf(subject, null, 'SameValueZero')); // 5
console.log(indexOf(subject, '2', 'SameValueZero')); // -1
console.log(indexOf(subject, NaN, 'SameValueZero')); // 10
console.log(indexOf(subject, -0, 'SameValueZero')); // 8
console.log(indexOf(subject, 2, 2, 'SameValueZero')); //6
// `SameValue` mode extends `indexOf` to match `NaN` and signed `0`.
console.log(indexOf(subject, null, 'SameValue')); // 5
console.log(indexOf(subject, '2', 'SameValue')); // -1
console.log(indexOf(subject, NaN, 'SameValue')); // 10
console.log(indexOf(subject, -0, 'SameValue')); // 9
console.log(indexOf(subject, 2, 2, 'SameValue')); //6