Find the lastIndexOf() an object with a key in array of objects

javascript find index of object in array by property
array lastindexof java
array get index
find element in array js
angularjs find index of object in array by id
filter index javascript
.findindex mdn
findindex is not a function

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;
}

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(const i = array.length - 1; i >= 0; i--){
    if(array[i].key === key)
      return i;
  }
  return -1;
};

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

is there a way to use .indexOf to find an object's value in an array , How do you find the index of an object in an array? If your object is the same object of the ones you are using within the array, you should be able to get the index of the Object in the same way you do as if it was a string.


I think you want something like below:

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

console.log(lastIndexOf("e", 2));

function lastIndexOf(keyValue, start) {
    for (var i = arr.length - 1; i >= start; i--) {
        if (arr[i].key === keyValue) {
            return i;
        }
    }
    return null;
}

How to check if an array includes an object in JavaScript , How do you check if an array contains an object JavaScript? 2 Answers. indexOf just gets the first index, if you want the value of an element try the find method. Using indexOf would have been feasible had your array only contained primitive types such as numbers, strings etc. but as your array contains objects, it needs a different approach.


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' );

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)

Array.prototype.lastIndexOf(), How do you find the last index of an array? I would like to find index in array. Positions in array are objects, and I want to filter on their properties. I know which keys I want to filter and their values. Problem is to get index of array which meets the criteria. For now I made code to filter data and gives me back object data, but not index of array.


Just try to find last index in the whole massive and compare it to start

let ind = array.lastIndexOf(object.key); 
if (ind > start) {return}

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  Finding the index of an object within an array by property value using PowerShell Today I was looking for an option to find the index of an object within an array of objects. Using an array of one dimensional objects like integers this is as simple as using the static IndexOf method of the Array class in the System namespace:


indexOf() . (It's similar to findIndex() , but checks each element for equality with the value instead of using a testing function.) If you need  If you want to use the plain old javascript you can create a function that iterates over the array and then return the object when the id matches with the one you’re looking for (or return null


indexOf to find "chocolate" or "chips" or even the price? etc. but as your array contains objects, it needs a different approach. Array.prototype.findIndex() we are not told the index, but we do find the other 'key:value' located  Stack lastIndexOf(Object, int) method in Java with Example The Java.util.Stack.lastIndexOf(Object element, int last_index) method is used to the last index of the first occurrence of the specified element in this Stack, searching forwards from last index, or returns -1 if the element is not found.


To access the index of the object from the array of an object by having a value map() This method creates a separate array and calls a function for every array element. Example-3: This example doesn't get the value so returns the -1 and prints Not Found! How to remove object from array of objects using JavaScript ? The lastIndexOf() method 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. The source for this interactive example is stored in a GitHub repository.