Packages@accelint/coreArray
indexOf
Returns the first index at which a given element can be found in an array
Usage
import { indexOf } from '@accelint/core';
// Find index of a value
const index = indexOf(3)([1, 2, 3, 4, 5]);
// 2
// Partial application
const findAppleIndex = indexOf('apple');
findAppleIndex(['banana', 'orange', 'apple']); // 2Reference
function indexOf<T>(
x: T
): (arr: T[]) => numberParameters
| Parameter | Type | Description |
|---|---|---|
x | T | The value to search for in the array. |
Returns
Returns a curried function that accepts an array and returns the index of the first occurrence of the value, or -1 if not found.
Type Parameters
T- The type of elements in the array and the value to search for
Examples
Example: Finding first occurrence
import { indexOf } from '@accelint/core';
const data = [1, 2, 3, 2, 1];
indexOf(2)(data); // 1 (first occurrence)Example: Element not found
import { indexOf } from '@accelint/core';
const numbers = [1, 2, 3, 4, 5];
indexOf(10)(numbers); // -1Example: Strict equality comparison
import { indexOf } from '@accelint/core';
// Objects are compared by reference
const obj = { id: 1 };
const arr = [obj, { id: 2 }, { id: 3 }];
indexOf(obj)(arr); // 0
indexOf({ id: 1 })(arr); // -1 (different object reference)Example: Working with different types
import { indexOf } from '@accelint/core';
const mixed: (string | number)[] = ['a', 1, 'b', 2];
indexOf('b')(mixed); // 2
indexOf(1)(mixed); // 1Example: Checking if element exists
import { indexOf } from '@accelint/core';
const hasElement = (value: number) => (arr: number[]) =>
indexOf(value)(arr) !== -1;
const numbers = [1, 2, 3, 4, 5];
hasElement(3)(numbers); // true
hasElement(10)(numbers); // falseGood to know: This function uses strict equality (
===) for comparison. For predicate-based searches, use findIndex instead.