Packages@accelint/coreArray
findIndex
Returns the index of the first element in an array that satisfies a predicate function
Usage
import { findIndex } from '@accelint/core';
// Find index of first even number
const firstEvenIndex = findIndex((x: number) => x % 2 === 0)([1, 2, 3, 4, 5]);
// 1
// Partial application
const findNegative = findIndex((x: number) => x < 0);
findNegative([5, 10, -3, 8]); // 2Reference
function findIndex<T>(
predicate: (element: T, index: number) => boolean
): (arr: T[]) => numberParameters
| Parameter | Type | Description |
|---|---|---|
predicate | (element: T, index: number) => boolean | Function called for each element until it returns true. Receives the element and its index. |
Returns
Returns a curried function that accepts an array and returns the index of the first matching element, or -1 if no match is found.
Type Parameters
T- The type of elements in the array
Examples
Example: Finding by property value
import { findIndex } from '@accelint/core';
interface Product {
id: number;
name: string;
inStock: boolean;
}
const products: Product[] = [
{ id: 1, name: 'Laptop', inStock: false },
{ id: 2, name: 'Mouse', inStock: true },
{ id: 3, name: 'Keyboard', inStock: true }
];
const firstInStockIndex = findIndex((p: Product) => p.inStock)(products);
// 1Example: Using index parameter
import { findIndex } from '@accelint/core';
// Find first element greater than its index
const data = [0, 0, 5, 2, 10];
const index = findIndex((value: number, idx: number) => value > idx)(data);
// 2 (value 5 > index 2)Example: Element not found
import { findIndex } from '@accelint/core';
const numbers = [1, 3, 5, 7, 9];
const evenIndex = findIndex((x: number) => x % 2 === 0)(numbers);
// -1 (no even numbers found)Good to know: The predicate is called only until a matching element is found. Once found, iteration stops immediately and the index is returned.
Related
- find - Find the first matching element
- findLastIndex - Find index from the end of array
- indexOf - Find index by value equality
- filter - Find all matching elements