Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  103] [ 3]  / answers: 1 / hits: 21770  / 7 Years ago, fri, september 22, 2017, 12:00:00

I'm looking for a way to find the last index of an object in Javascript from a point in an array. For example:



array.lastIndexOf(object.key, start);


So far, I haven't found a good solution for this problem. I could splice the array from the 'start' point, reverse it, and then search the sliced array for the key value, but this seems like an inefficient solution to me.



EDIT:



To illustrate the problem a little more, I'm posting the code that I used in the end to solve the problem. Essentially; what I did was I used While to loop through the previous values in the array.



getLastValue = (index) => {
const arr = [
{'d':'01-02-2017','v':'123'},
{'d':'02-02-2017'},
{'d':'04-02-2017'},
{'d':'05-02-2017','v':'456'},
...
];

let lastValue;

while (arr[index] && !arr[index].v) {
index--;
}

lastValue = arr[index];

return lastValue;
}

More From » arrays

 Answers
35

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

[#56413] Wednesday, September 20, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiej

Total Points: 294
Total Questions: 95
Total Answers: 97

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;