The lodash method _.indexOf exported as a module.
npx @tessl/cli install tessl/npm-lodash--indexof@4.0.00
# Lodash IndexOf
1
2
The lodash.indexof package provides the lodash `_.indexOf` method as a standalone Node.js module, enabling efficient array searching functionality with SameValueZero equality comparisons.
3
4
## Package Information
5
6
- **Package Name**: lodash.indexof
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.indexof` (recommended: `npm i -g npm` first to update npm)
10
- **Version**: 4.0.5
11
12
## Core Imports
13
14
```javascript
15
const indexOf = require('lodash.indexof');
16
```
17
18
For ES modules (with compatible bundlers):
19
20
```javascript
21
import indexOf from 'lodash.indexof';
22
```
23
24
## Basic Usage
25
26
```javascript
27
const indexOf = require('lodash.indexof');
28
29
// Basic search
30
indexOf([1, 2, 1, 2], 2);
31
// => 1
32
33
// Search from specific index
34
indexOf([1, 2, 1, 2], 2, 2);
35
// => 3
36
37
// Value not found
38
indexOf([1, 2, 3], 4);
39
// => -1
40
41
// Handle NaN values correctly
42
indexOf([1, NaN, 3], NaN);
43
// => 1
44
```
45
46
## Capabilities
47
48
### Array Index Search
49
50
Gets the index at which the first occurrence of a value is found in an array using SameValueZero equality comparisons. If fromIndex is negative, it's used as the offset from the end of array.
51
52
```javascript { .api }
53
/**
54
* Gets the index at which the first occurrence of `value` is found in `array`
55
* using SameValueZero for equality comparisons. If `fromIndex` is negative,
56
* it's used as the offset from the end of `array`.
57
*
58
* @param {Array} array - The array to inspect
59
* @param {*} value - The value to search for
60
* @param {number} [fromIndex=0] - The index to search from
61
* @returns {number} Returns the index of the matched value, else -1
62
*/
63
function indexOf(array, value, fromIndex);
64
```
65
66
**Parameters:**
67
- `array` (Array): The array to inspect
68
- `value` (*): The value to search for
69
- `fromIndex` (number, optional): The index to search from (default: 0)
70
71
**Returns:**
72
- (number): The index of the matched value, else -1
73
74
**Usage Examples:**
75
76
```javascript
77
const indexOf = require('lodash.indexof');
78
79
// Basic usage
80
indexOf([1, 2, 1, 2], 2);
81
// => 1
82
83
// Search from specific index
84
indexOf([1, 2, 1, 2], 2, 2);
85
// => 3
86
87
// Negative fromIndex (offset from end)
88
indexOf([1, 2, 1, 2], 1, -2);
89
// => 2
90
91
// Handle special values
92
indexOf([1, NaN, 3], NaN); // Uses SameValueZero
93
// => 1
94
95
indexOf([1, undefined, 3], undefined);
96
// => 1
97
98
indexOf([1, null, 3], null);
99
// => 1
100
101
// Empty array
102
indexOf([], 1);
103
// => -1
104
105
// Value not found
106
indexOf([1, 2, 3], 'a');
107
// => -1
108
```
109
110
## Implementation Details
111
112
The function uses SameValueZero equality comparison, which means:
113
- It correctly identifies NaN values (unlike strict equality `===`)
114
- It treats +0 and -0 as equal
115
- It handles edge cases with undefined and null values consistently
116
117
The implementation includes optimizations for:
118
- Negative fromIndex values (converted to positive offset)
119
- Bounds checking to prevent out-of-range errors
120
- Special NaN handling through internal `baseIsNaN` function
121
- Performance optimization through direct array access patterns
122
123
## Additional Resources
124
125
- [Official lodash documentation](https://lodash.com/docs#indexOf)
126
- [Package source on GitHub](https://github.com/lodash/lodash/blob/4.0.5-npm-packages/lodash.indexof)