0

Code below will return me the first index of the object with id = "1" which is 0. How to find the last index of the object with id = "1" which is 2 ? Of course I could use a loop but without using a loop is it possible?

var array = [{id: "1"}, {id: "2"}, {id: "1"}];

    var index = array.findIndex(function(oItem) {
        return oItem.id === "1";
    });
1
  • 1
    you could use recursion.... Commented Apr 7, 2017 at 15:43

4 Answers 4

3

You can map() you array to just array of ids and then use lastIndexOf

var array = [{id: "1"}, {id: "2"}, {id: "1"}];

var lastIndex = array.map(e => e.id).lastIndexOf('1')
console.log(lastIndex)

Since currently there is no findLastIndex method and you don't want to use loop you could create your own function using recursion that you can pass anonymous function as parameter to search by.

var array = [{id: "1"}, {id: "2"}, {id: "1"}];

function findLastIndex(data, func) {
  var start = data.length - 1;
  var result = null;

  function search(data, i, func) {
    if (i < 0) return;
    var check = func(data[i]);

    if (check == true) {
      result = i;
      return;
    }
    search(data, i -= 1, func)
  }

  search(data, start, func)
  return result;
}

var lastIndex = findLastIndex(array, function(e) {
  return e.id == 1;
})

console.log(lastIndex)

Sign up to request clarification or add additional context in comments.

14 Comments

From the OP: "Of course I could use a loop but without using a loop is it possible?"
I am quite new to Javascript coming from ABAP background. It looks to me like all the operations done on arrays are based on looping through them. Are there any binary searches on arrays out of the box?
@9000 map is also a loop, isn't it?
Nice functional solution. Short circuits the search. I might make it a bit shorter and cleaner by returning the index without using a closure: jsfiddle.net/193tbp7k
@Radek: Then if you sorted the array, you could do the binary search. There's nothing built in, so you'll need to write one. But if you don't ultimately need them to be kept in an Array, then once you get the data, you could convert it to a Map, where the key is the ID and the value is an array of matches for that ID. If you need lookups on other properties too, then you'll want to take that into account.
|
1

You could use Array#map and declare new index variable, which would be overwrited until it reaches the last element with given id.

var array = [{id: "1"}, {id: "2"}, {id: "1"}],
    index;
    arr = array.map((v,i) => v.id == 1 ? index = i : null);
    
    console.log(index);

Comments

0

Try this using Array map() method :

var myarray = [{id: "1"}, {id: "2"}, {id: "1"}];

var res = myarray.map(function(item) {
  return item.id;
});

console.log(res.lastIndexOf("1"));

Comments

-1

If you don't mind using lodash, it has findLastIndex.

If anything, you can look at the source of it for ideas.

2 Comments

I didn't downvote, but I guess suggesting a library where at worst a loop would do is a bit overkill.
@strah: This is a kind of library you end up using anyway in most projects — but not necessarily. This is why it starts with an "If". Unfortunately, the standard Array object lacks an efficient method for that. A library may pick the most perfromant way for a particular environment, though, should it exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.