Searching Arrays with indexOf and lastIndexOf
Examples
Using lastIndexOf()
The following example uses lastIndexOf()
to locate values in an array.
const numbers = [2, 5, 9, 2]; numbers.lastIndexOf(2); // 3 numbers.lastIndexOf(7); // -1 numbers.lastIndexOf(2, 3); // 3 numbers.lastIndexOf(2, 2); // 0 numbers.lastIndexOf(2, -2); // 0 numbers.lastIndexOf(2, -1); // 3
You cannot use lastIndexOf()
to search for NaN
.
const array = [NaN]; array.lastIndexOf(NaN); // -1
Finding all the occurrences of an element
The following example uses lastIndexOf
to find all the indices of an
element in a given array, using push
to add them
to another array as they are found.
const indices = []; const array = ["a", "b", "a", "c", "a", "d"]; const element = "a"; let idx = array.lastIndexOf(element); while (idx !== -1) { indices.push(idx); idx = idx > 0 ? array.lastIndexOf(element, idx - 1) : -1; } console.log(indices); // [4, 2, 0]
Note that we have to handle the case idx === 0
separately here because the
element will always be found regardless of the fromIndex
parameter if it is
the first element of the array. This is different from the
indexOf
method.
Using lastIndexOf() on sparse arrays
You cannot use lastIndexOf()
to search for empty slots in sparse arrays.
console.log([1, , 3].lastIndexOf(undefined)); // -1
Calling lastIndexOf() on non-array objects
The lastIndexOf()
method reads the length
property of this
and then accesses each property whose key is a nonnegative integer less than length
.
const arrayLike = { length: 3, 0: 2, 1: 3, 2: 2, 3: 5, // ignored by lastIndexOf() since length is 3 }; console.log(Array.prototype.lastIndexOf.call(arrayLike, 2)); // 2 console.log(Array.prototype.lastIndexOf.call(arrayLike, 5)); // -1