Lodash _.findLastIndex() method
What you’ll learn
- How
_.findLastIndex(collection, predicate, [fromIndex])returns the last matching index in reverse scan order, or-1. - When to prefer it over _.findIndex() for duplicates, logs, or timeline data.
- How it lines up with native
findLastIndex(ES2023) and Lodash iteratee shorthand. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.findIndex() on this site first; both APIs share the same predicate and iteratee rules.
- You understand that “last” refers to the greatest index that matches when scanning from the end of the array.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.findLastIndex is the right-to-left twin of _.findIndex: it finds the last element satisfying your predicate. That is the tool you want for “most recent” rows, trailing flags, or the final duplicate in a list before you splice or split.
Reverse scan
Evaluation starts near the end of the array and moves toward index 0, so later duplicates win.
Non-destructive
The collection is not mutated; you only receive an index or -1.
Same iteratees
Functions, property paths, and partial objects work just like on findIndex.
Syntax
_.findLastIndex(collection, [predicate=_.identity], [fromIndex=collection.length - 1]) - collection: array or array-like value scanned from a starting index toward
0. - predicate: invoked as
(value, index, collection); the first truthy result encountered during the reverse walk wins. Lodash iteratee shorthand applies. - fromIndex: where the reverse scan begins (defaults to the last index).
- Returns: the index of the matched element, or
-1if none.
Last index with a custom test
When the same value appears more than once, findLastIndex returns the rightmost match.
import findLastIndex from "lodash/findLastIndex";
findLastIndex([1, 2, 3, 2, 4], (x) => x === 2);
// 3 Partial object iteratee
With duplicate records, the partial object form still resolves to the last row that matches.
import findLastIndex from "lodash/findLastIndex";
const rows = [{ id: 1 }, { id: 2 }, { id: 2 }];
findLastIndex(rows, { id: 2 });
// 2 Bound the reverse search
fromIndex sets where the reverse walk starts. Here the last 2 in the full array would be index 3, but starting at 2 stops the search before that slot.
import findLastIndex from "lodash/findLastIndex";
findLastIndex([1, 2, 3, 2], (x) => x === 2, 2);
// 1 📋 _.findLastIndex vs _.findIndex vs native
| API | Scan direction | Note |
|---|---|---|
_.findLastIndex | End toward start | Last match; iteratee shorthand; fromIndex caps where the reverse pass begins |
_.findIndex | Start toward end | First match; same shorthands |
Array.prototype.findLastIndex | End toward start | Native ES2023; callback only (no Lodash partial-object iteratee) |
Pitfalls to avoid
Not the largest index in the whole array
It is the last match in the reverse scan from fromIndex. A smaller fromIndex can skip matches that sit to the right.
Same as findIndex
NaN never satisfies ===; use Number.isNaN or _.isNaN inside the predicate when hunting for NaN slots.
Guard splice and slice
Treat -1 like “not found” before passing the index into APIs that interpret negative numbers specially.
❓ FAQ
Summary
- Purpose:
_.findLastIndex(collection, predicate, fromIndex)returns the index of the last element matching the predicate in reverse scan order, or-1. - Safety: the input collection is not mutated by this call.
- Next: Lodash _.flatten(), _.findIndex() (earlier in this track), or the array methods hub.
ES2023 added native Array.prototype.findLastIndex; Lodash’s version still helps when you want the same iteratee shorthands and consistent behavior across older runtimes.
6 people found this page helpful
