Personally, I wouldn't choose either solution. Here is why:

LastIndexOf:

The problem lies in the comparing of elements while searching through the array. It does compare the elements using strict equality. Therefore comparing objects will always fail, except they are the same. In OP case they are different.

Slice & reverse one-liner @adeneo

Given an array of three elements [{key: A},{key: B},{key: C}] and the lookup for the last index of key = D will give you an index of 3. This is wrong as the last index should be -1 (Not found)

Looping through the array

While this is not necessarily wrong, looping through the whole array to find the element isn't the most concise way to do it. It's efficient yes, but readability can suffer from it. If I had to choose one, I'd probably choose this one. If readability / simplicity is your friend, then below is yet one more solution.


A simple solution

We can make lastIndexOf work, we just need to make the value comparable (strict equality conform). Or simply put: we need to map the objects to a single property that we want to find the last index of using javascript's native implementation.

const arr = [ { key: "a" }, { key: "b" }, { key: "c" }, { key: "e" }, { key: "e" }, { key: "f" } ];

arr.map(el => el.key).lastIndexOf("e"); //4
arr.map(el => el.key).lastIndexOf("d"); //-1

// Better:
const arrKeys = arr.map(el => el.key);
arrKeys.lastIndexOf("c"); //2
arrKeys.lastIndexOf("b"); //1

A fast solution

Simple backwards lookup (as concise and as fast as possible). Note the -1 return instead of null/undefined.

const arr = [ { key: "a" }, { key: "b" }, { key: "c" }, { key: "e" }, { key: "e" }, { key: "f" } ];

const lastIndexOf = (array, key) => {
  for(let i = array.length - 1; i >= 0; i--){
    if(array[i].key === key)
      return i;
  }
  return -1;
};

lastIndexOf(arr, "e"); //4
lastIndexOf(arr, "x"); //-1
Answer from Tom Siwik on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › lastIndexOf
String.prototype.lastIndexOf() - JavaScript | MDN
The lastIndexOf() method of String values searches this string and returns the index of the last occurrence of the specified substring. It takes an optional starting position and returns the last occurrence of the specified substring at an index less than or equal to the specified number.
🌐
W3Schools
w3schools.com › jsref › jsref_lastindexof.asp
JavaScript String lastIndexOf() Method
The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string.
Discussions

javascript - Find the lastIndexOf() an object with a key in array of objects - Stack Overflow
We can make lastIndexOf work, we just need to make the value comparable (strict equality conform). Or simply put: we need to map the objects to a single property that we want to find the last index of using javascript's native implementation. More on stackoverflow.com
🌐 stackoverflow.com
Slice/lastIndexOf method in javascript
Ok, lets start with 'lastIndexOf'. Imagine this const array = ['a', 'a', 'b', 'b', 'c', 'c', 'c'] Now what they ask you to do is tell them where in that array the last 'b' is located. So you do array.lastIndexOf('b') Which will give you the number 3, since the first position is 0. Now you can work with that information. Lets say you want to clear all the old data. The new data is the fancy 'c' the client wants, everything else is to be ignored. Slice takes two arguments, start of what you want to extract and end, but end is optional, which means you can just use the start. So if you know that the 'c' starts after 'b', your start will be the position after 'b'. lastIndexOf tells you where 'b' is, so you can combine them like this: const newArray = array.slice(array.lastIndexOf('b') + 1) This will return only the three 'c's. If you had used slice(2, 4), it would start at the 3rd element, which is a 'b', include the 4th element, also a 'b' and end at the 5th element (index 4). So it would ignore the rest and the new array would be ['b', 'b', 'c']. I'm on the phone in the train, so this could be poorly formatted and maybe not well explained, please let me know if so, I'll try again. More on reddit.com
🌐 r/learnjavascript
5
1
August 28, 2023
String.prototype.lastIndexOf() - Last Index Of Explained with Examples - Guide - The freeCodeCamp Forum
The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found. The calling string is searched backward, starting at fromIndex . Syntax s… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
July 25, 2019
javascript - Difference between string.indexOf() and string.lastIndexOf()? - Stack Overflow
A pure JavaScript function is an implementation in JavaScript itself :) 2014-08-18T06:18:06.967Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... The 2025 Stack Overflow and Stack Exchange wrap—our top ten questions of the... ... Stack Overflow chat opening up to all users in January; Stack Exchange chat... Modernizing curation: A proposal for The Workshop and The Archive · 4 How is lastIndexOf... More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › lastIndexOf
Array.prototype.lastIndexOf() - JavaScript | MDN
The lastIndexOf() method of Array instances returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-lastindexof-method
JavaScript String lastIndexOf() Method - GeeksforGeeks
July 16, 2024 - It then uses the lastIndexOf() method to find the index of the last occurrence of the substring 'train' (case-sensitive) within the string str. Since 'train' is not found in 'Departed Train', -1 is printed to the console.
Top answer
1 of 7
19

Personally, I wouldn't choose either solution. Here is why:

LastIndexOf:

The problem lies in the comparing of elements while searching through the array. It does compare the elements using strict equality. Therefore comparing objects will always fail, except they are the same. In OP case they are different.

Slice & reverse one-liner @adeneo

Given an array of three elements [{key: A},{key: B},{key: C}] and the lookup for the last index of key = D will give you an index of 3. This is wrong as the last index should be -1 (Not found)

Looping through the array

While this is not necessarily wrong, looping through the whole array to find the element isn't the most concise way to do it. It's efficient yes, but readability can suffer from it. If I had to choose one, I'd probably choose this one. If readability / simplicity is your friend, then below is yet one more solution.


A simple solution

We can make lastIndexOf work, we just need to make the value comparable (strict equality conform). Or simply put: we need to map the objects to a single property that we want to find the last index of using javascript's native implementation.

const arr = [ { key: "a" }, { key: "b" }, { key: "c" }, { key: "e" }, { key: "e" }, { key: "f" } ];

arr.map(el => el.key).lastIndexOf("e"); //4
arr.map(el => el.key).lastIndexOf("d"); //-1

// Better:
const arrKeys = arr.map(el => el.key);
arrKeys.lastIndexOf("c"); //2
arrKeys.lastIndexOf("b"); //1

A fast solution

Simple backwards lookup (as concise and as fast as possible). Note the -1 return instead of null/undefined.

const arr = [ { key: "a" }, { key: "b" }, { key: "c" }, { key: "e" }, { key: "e" }, { key: "f" } ];

const lastIndexOf = (array, key) => {
  for(let i = array.length - 1; i >= 0; i--){
    if(array[i].key === key)
      return i;
  }
  return -1;
};

lastIndexOf(arr, "e"); //4
lastIndexOf(arr, "x"); //-1
2 of 7
4

With ES2015 and findIndex you can pass a callback to look for an objects key.

If you make a copy of the array, and reverse it, you can find the last one by subtracting that index from the total length (and 1, as arrays are zero based)

It's not very efficient, but it's one line, and works well for normally sized arrays i.e. not a million indices

var idx = arr.length - 1 - arr.slice().reverse().findIndex( (o) => o.key == 'key' );

Show code snippet

var arr = [{key : 'not'}, {key : 'not'}, {key : 'key'}, {key : 'not'}];

var idx = arr.length - 1 - arr.slice().reverse().findIndex( (o) => o.key == 'key' ); // 2

console.log(idx)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

A more efficient approach would be to iterate backwards until you find the object you're looking for, and break the loop

var arr = [{key: 'not'}, {key: 'not'}, {key: 'key'}, {key: 'not'}];

var idx = (function(key, i) {
  for (i; i--;) {
    if (Object.values(arr[i]).indexOf(key) !== -1) {
      return i;
      break;
    }
  }   return -1;
})('key', arr.length);

console.log(idx)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

🌐
Reddit
reddit.com › r/learnjavascript › slice/lastindexof method in javascript
r/learnjavascript on Reddit: Slice/lastIndexOf method in javascript
August 28, 2023 -

Can anyone explain what slice and lastIndexOf does in JavaScript. I searched it up but the explanation is too hard to understand.

Top answer
1 of 3
3
Ok, lets start with 'lastIndexOf'. Imagine this const array = ['a', 'a', 'b', 'b', 'c', 'c', 'c'] Now what they ask you to do is tell them where in that array the last 'b' is located. So you do array.lastIndexOf('b') Which will give you the number 3, since the first position is 0. Now you can work with that information. Lets say you want to clear all the old data. The new data is the fancy 'c' the client wants, everything else is to be ignored. Slice takes two arguments, start of what you want to extract and end, but end is optional, which means you can just use the start. So if you know that the 'c' starts after 'b', your start will be the position after 'b'. lastIndexOf tells you where 'b' is, so you can combine them like this: const newArray = array.slice(array.lastIndexOf('b') + 1) This will return only the three 'c's. If you had used slice(2, 4), it would start at the 3rd element, which is a 'b', include the 4th element, also a 'b' and end at the 5th element (index 4). So it would ignore the rest and the new array would be ['b', 'b', 'c']. I'm on the phone in the train, so this could be poorly formatted and maybe not well explained, please let me know if so, I'll try again.
2 of 3
2
You can find the docs for these methods here: String: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf Array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
Find elsewhere
🌐
Medium
medium.com › @amirakhaled2027 › demystifying-lastindexof-and-indexof-mastering-string-search-in-javascript-bd06d9c7429d
Demystifying lastIndexOf and indexOf: Mastering String Search in JavaScript | by Amira Khaled | Medium
May 27, 2024 - In this example, the indexOf method ... lastIndexOf method, in contrast, searches a string for a specified substring and returns the index of the last occurrence of the substring within the string....
🌐
freeCodeCamp
forum.freecodecamp.org › guide
String.prototype.lastIndexOf() - Last Index Of Explained with Examples - Guide - The freeCodeCamp Forum
July 25, 2019 - The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found. The calling string is searched backward, starting at fromIndex .
🌐
Codecademy
codecademy.com › docs › javascript › strings › .lastindexof()
JavaScript | Strings | .lastIndexOf() | Codecademy
November 24, 2025 - The .lastIndexOf() method in JavaScript returns the position of the last occurrence of a specified substring within a string. If the substring is not found, it returns -1.
🌐
Programiz
programiz.com › javascript › library › string › lastindexof
JavaScript String lastIndexOf() (With Examples)
In the above example, we have defined a string named str. The substr variable contains "m" which is a substring of the given string. The lastIndexOf() method locates the index of the last occurrence of substr.
🌐
HCL Software
help.hcl-software.com › dom_designer › 9.0.1 › reference › r_wpdr_standard_string_lastindexof_r.html
lastIndexOf (JavaScript)
Gets the index of the last occurrence of a substring. String (Standard - JavaScript) lastIndexOf(searchString:string, position:int) : int · lastIndexOf(searchString:string) : int · This method searches backwards starting from the last position in the string, or the last position minus the ...
🌐
Quora
quora.com › What-is-the-difference-between-array-indexOf-and-lastIndexOf-in-JavaScript
What is the difference between array indexOf and lastIndexOf in JavaScript? - Quora
* The above two methods are JS String methods. [code]/* The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found.
🌐
Programiz
programiz.com › javascript › library › array › lastindexof
JavaScript Array lastIndexOf() (with Examples)
In this tutorial, you will learn about the JavaScript Array lastIndexOf() method with the help of examples. The lastIndexOf() method returns the index of the last occurrence of a specified element in the array.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.lastindexof()
JavaScript String lastIndexOf() Method
November 3, 2024 - In this tutorial, you'll learn how to use the JavaScript String lastIndexOf() method to locate the last occurrence of a substring in a string.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › lastIndexOf
JavaScript String lastIndexOf() - Find Last Index | Vultr Docs
May 15, 2025 - In this article, you will learn how to utilize the lastIndexOf() method of JavaScript to find the last index of a character or substring in a string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › lastIndexOf
TypedArray.prototype.lastIndexOf() - JavaScript | MDN
The lastIndexOf() method of TypedArray instances returns the last index at which a given element can be found in the typed array, or -1 if it is not present. The typed array is searched backwards, starting at fromIndex.
🌐
Scaler
scaler.com › home › topics › javascript string lastindexof()
JavaScript String lastIndexOf() - Scaler Topics
February 14, 2024 - The lastIndexOf method in javascript returns the index of the last occurrence of the specified substring. Given a second argument, a number, the lastIndexOf() method returns the last occurrence of the specified substring at an index less than ...
🌐
Mimo
mimo.org › glossary › javascript › string-indexof-method
JavaScript String indexOf() method: Syntax, Usage, and Examples
The indexOf() method in JavaScript allows you to find the position of a substring within a string. It returns the index of the first occurrence or -1 if the substring doesn’t exist.
🌐
TechOnTheNet
techonthenet.com › js › string_lastindexof.php
JavaScript: String lastIndexOf() method
Even though the lastIndexOf() method searches the string backwards, it still returns a location value that is relative to the start of the string. For example, a return value of 0 is the location of the first character in the string, a return value of 1 is the location of the second character in the string, and so on.