Let’s say I have this array:
var a = [1,2,99,3,4,99,5];
I would like to get the position of the second 99, something like:
a.indexOf(99, 2) // --> 5
However the second argument in indexOf just tells where to start the search. Is there any built in functions to make this? If not how would you do it?
Thanks!
There’s only
indexOfandlastIndexOf. You could loop over it:If you always want the second occurrence Jan’s method is also good:
The
indexOfcall on the right finds the first occurrence,+1then limits the search to the elements that follow it.