You can loop backward:
const aLastIndexOf = (str, ch) => {
for (let index = str.length - 1; index >= 0; index--) {
if (str[index] == ch)
return index;
}
return -1;
}
An example:
const aLastIndexOf = (str, ch) => {
for (let index = str.length - 1; index >= 0; index--) {
if (str[index] == ch)
return index;
}
return -1;
}
console.log(aLastIndexOf("hello", 'h'));
console.log(aLastIndexOf("hello", 'l'));
console.log(aLastIndexOf("hello", 'e'));
Answer from StepUp on Stack Overflowjavascript - Alternative to the lastIndexOf() function - Stack Overflow
Slice/lastIndexOf method in javascript
javascript - Find the lastIndexOf() an object with a key in array of objects - Stack Overflow
string.lastIndexOf()
Videos
You can loop backward:
const aLastIndexOf = (str, ch) => {
for (let index = str.length - 1; index >= 0; index--) {
if (str[index] == ch)
return index;
}
return -1;
}
An example:
const aLastIndexOf = (str, ch) => {
for (let index = str.length - 1; index >= 0; index--) {
if (str[index] == ch)
return index;
}
return -1;
}
console.log(aLastIndexOf("hello", 'h'));
console.log(aLastIndexOf("hello", 'l'));
console.log(aLastIndexOf("hello", 'e'));
a simple for loop can solve your problem
function stringLastIndexOf(str1, str2) {
let index = -1
for (var i = 0; i < str1.length; i++) {
if (str1[i] === str2) {
index = i
}
}
return index
}
console.log(
stringLastIndexOf('word', 'w'),
stringLastIndexOf('pumped', 'f')
);
Can anyone explain what slice and lastIndexOf does in JavaScript. I searched it up but the explanation is too hard to understand.
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
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