Lodash _.findIndex() method
What you’ll learn
- How
_.findIndex(collection, predicate, [fromIndex=0])returns the first matching index or-1. - Using callbacks, partial-object iteratees, and an optional
fromIndexsearch start. - How this compares to native
Array.prototype.findIndexand to_.find(value vs index). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional prior read _.fill() on this site; you should be comfortable with array indexes and truthy versus falsy values.
- You can read a predicate as
(value, index, collection) => booleanwithout mutating arguments. - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.findIndex answers “where is the first match?” by returning a numeric index. It pairs naturally with slice, splice, or immutable updates that need a position. Unlike _.find, you get an index (or -1), not the element itself.
Non-destructive
The source collection is read-only for this call; only the returned number reflects the outcome.
Predicate-first
Express arbitrary tests; use iteratee shorthand when you only need property or partial-object matching.
fromIndex
Skip early matches when you need the next occurrence after a known position.
Syntax
_.findIndex(collection, [predicate=_.identity], [fromIndex=0]) - collection: array or array-like value scanned from low indexes upward.
- predicate: invoked as
(value, index, collection); the first truthy result selects that index. Lodash also accepts iteratee shorthand (string path, partial object, and so on). - fromIndex: where to begin searching (defaults to
0). - Returns: the zero-based index of the first match, or
-1if none.
First index with a custom test
The predicate runs left to right; the first element for which it is truthy determines the returned index.
import findIndex from "lodash/findIndex";
findIndex([10, 20, 30, 40], (x) => x > 25);
// 2 Partial object iteratee
Pass an object to match elements that contain the same own properties (Lodash “matches” shorthand).
import findIndex from "lodash/findIndex";
const rows = [
{ id: 1, role: "guest" },
{ id: 2, role: "admin" }
];
findIndex(rows, { id: 2 });
// 1 Start the search later
With fromIndex, skip earlier matches; here the first 2 is at index 0, but the search begins at 1.
import findIndex from "lodash/findIndex";
findIndex([2, 1, 2, 3], (x) => x === 2, 1);
// 2 📋 _.findIndex vs native findIndex
| API | Why Lodash | Note |
|---|---|---|
_.findIndex(collection, predicate, fromIndex) | Iteratee shorthand, consistent fromIndex behavior, works with array-like values in one style | Prefer when the rest of your module already imports Lodash helpers |
Array.prototype.findIndex | Built-in, zero dependency | Great on real arrays when you only need a callback (no partial-object iteratee) |
_.find | Returns the matched value (or undefined) | Use findIndex when you specifically need the index |
Pitfalls to avoid
Strict equality surprises
x === NaN is always false. Use Number.isNaN inside the predicate, or rely on Lodash helpers such as _.isNaN where appropriate.
Partial objects are not deep clones
Iteratee objects perform per-property checks; they are not a substitute for a custom comparator when you need deep or ordered structure equality.
Always guard before use
Passing -1 into splice or as an object key behaves differently from a valid index; branch on idx !== -1 first.
❓ FAQ
Summary
- Purpose:
_.findIndex(collection, predicate, fromIndex)returns the first index where the predicate is truthy, or-1. - Safety: the input collection is not mutated by this call.
- Next: Lodash _.findLastIndex(), _.fill() (earlier in this track), or the array methods hub.
Lodash _.findIndex accepts the same rich iteratee shorthands as other collection methods (functions, property paths, partial objects), while still returning a plain numeric index or -1.
6 people found this page helpful
