CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-indexof

The lodash method _.indexOf exported as a module.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Lodash IndexOf

The lodash.indexof package provides the lodash _.indexOf method as a standalone Node.js module, enabling efficient array searching functionality with SameValueZero equality comparisons.

Package Information

  • Package Name: lodash.indexof
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.indexof (recommended: npm i -g npm first to update npm)
  • Version: 4.0.5

Core Imports

const indexOf = require('lodash.indexof');

For ES modules (with compatible bundlers):

import indexOf from 'lodash.indexof';

Basic Usage

const indexOf = require('lodash.indexof');

// Basic search
indexOf([1, 2, 1, 2], 2);
// => 1

// Search from specific index
indexOf([1, 2, 1, 2], 2, 2);  
// => 3

// Value not found
indexOf([1, 2, 3], 4);
// => -1

// Handle NaN values correctly
indexOf([1, NaN, 3], NaN);
// => 1

Capabilities

Array Index Search

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.

/**
 * Gets the index at which the first occurrence of `value` is found in `array`
 * using SameValueZero for equality comparisons. If `fromIndex` is negative, 
 * it's used as the offset from the end of `array`.
 *
 * @param {Array} array - The array to inspect
 * @param {*} value - The value to search for  
 * @param {number} [fromIndex=0] - The index to search from
 * @returns {number} Returns the index of the matched value, else -1
 */
function indexOf(array, value, fromIndex);

Parameters:

  • array (Array): The array to inspect
  • value (*): The value to search for
  • fromIndex (number, optional): The index to search from (default: 0)

Returns:

  • (number): The index of the matched value, else -1

Usage Examples:

const indexOf = require('lodash.indexof');

// Basic usage
indexOf([1, 2, 1, 2], 2);
// => 1

// Search from specific index
indexOf([1, 2, 1, 2], 2, 2);
// => 3

// Negative fromIndex (offset from end)
indexOf([1, 2, 1, 2], 1, -2);
// => 2

// Handle special values
indexOf([1, NaN, 3], NaN);  // Uses SameValueZero
// => 1

indexOf([1, undefined, 3], undefined);
// => 1

indexOf([1, null, 3], null);
// => 1

// Empty array
indexOf([], 1);
// => -1

// Value not found
indexOf([1, 2, 3], 'a');
// => -1

Implementation Details

The function uses SameValueZero equality comparison, which means:

  • It correctly identifies NaN values (unlike strict equality ===)
  • It treats +0 and -0 as equal
  • It handles edge cases with undefined and null values consistently

The implementation includes optimizations for:

  • Negative fromIndex values (converted to positive offset)
  • Bounds checking to prevent out-of-range errors
  • Special NaN handling through internal baseIsNaN function
  • Performance optimization through direct array access patterns

Additional Resources

  • Official lodash documentation
  • Package source on GitHub
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.indexof@4.0.x
Publish Source
CLI
Badge
tessl/npm-lodash-indexof badge