lastIndexOf()
lastIndexOf<
T>(array,value,fromIndex?):number
Gets the index at which the last occurrence of value is found in array.
DEPRECATED
Use array.lastIndexOf(value) directly instead.
Reason:
Native equivalent method now available
Type Parameters
T: T
The type of elements in the array.
Parameters
array: T[]
The array to search.
value: T
The value to search for.
fromIndex?: number
The index to search from.
Returns: number
The index of the matched value, or -1 if not found.
See Also
Since
2.0.0
Also known as
findLastIndex (Effect) · lastIndexOf (Lodash, es-toolkit, Ramda) · ❌ (Remeda, Radashi, Modern Dash, Antfu)
Example
const numbers = [1, 2, 3, 2, 4];
// ❌ Deprecated approach
const index = lastIndexOf(numbers, 2);
console.log(index); // 3
// ✅ Recommended approach
const indexNative = numbers.lastIndexOf(2);
console.log(indexNative); // 3
How it works?
Returns the index of the last occurrence of a value.
Deprecated: Use array.lastIndexOf() directly.
Native Equivalent
// ❌ lastIndexOf(arr, value)
// ✅ arr.lastIndexOf(value)
Use Cases
Find last occurrence 📌
Get the index of the last matching element.
const items = ["a", "b", "a", "c", "a"];
items.lastIndexOf("a");
// => 4
Find last match before position
Search backwards from a specific index.
const chars = ["a", "b", "a", "c", "a"];
chars.lastIndexOf("a", 3);
// => 2
Check duplicate position
Find if element appears multiple times.
const values = [1, 2, 3, 2, 4];
values.indexOf(2) !== values.lastIndexOf(2);
// => true (duplicates exist)