cme8ng
Last Updated: February 25, 2016
·
529
· richardgong1987
Psb  1

javascript navtive indexOf(), lastIndexOf() methods

ECMAScript 5 add two item location methods to array instance, indexOf(), lastIndexOf();
Each of these methods accepts two arguments,first one is the item look for,the second is an optional index from which to start looking;

here are some example:

<script>
    arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
   console.log(arr.indexOf('a'), arr.lastIndexOf('a'));//0 8 
</script>

ok,IndexOf() method starts searching from front of the array(item 0) and continues to the back,so,in above example indexOf() take a result "0",whereas lastIndexOf() starts from the last item in the array and continues to the front, so here lastIndexOf take a result "8"

i belive there lots of people would mistake this " lastIndexOf() starts from the last item in the array and continues to the front" statment;

 arr = ['a', 'b', 'c', 'c', 'b', 'b', 'b', 'a', 'a'],
console.log(arr.indexOf('b'), arr.lastIndexOf('b'));//1 6 

Although lastIndexOf() method "starts from the last item in the array and continues to the front" ,but that's doesn't mean searching stop in the second "b", "starts from the last item in the array and continues to the front",mean the "Javascript Rendering Engine" execution sequence that's start from last item to front item;

let's me prove to you:

 arr = [1, 2, 3, 4, 4, 5, 5, 2, 2, 2, 2 , 1, 1],
console.log(arr.indexOf(5), arr.lastIndexOf(5));//5 6 

lastIndexOf() is used searching last one item in the array;

arr = [1, 2, 3, 4, 4, 5, 5, 2, 2, 2, 2 , 1, 1],
console.log(arr.indexOf(5, 1), arr.lastIndexOf(5, 1));//5 -1 

why the result is 5 and -1 ? that's cos the indexOf() starts searching position is 5, however indexOf() method execution sequence is from front to back, here next item is 5,so result is 5;

but lastIndex() method execution sequence is from back to front, here the start position is 5, but in front direction there's can't searching "5", so result is -1;

indexOf(), lastIndexOf() methods not only be used in array, they can be used in others Object, like the String, and the they not only searhing string-like item,they can searhing Object

arr = 'abcdd'
console.log(arr.indexOf('d'), arr.lastIndexOf('d'));//3 4 

     var a = { 1: 1, 4: 4, 5: 5 };
     arr = [a, a, { 1: 1, 4: 4, 5: 55 }, { 1: 1, 4: 4, 5: 555}];        
    console.log(arr.indexOf(a), arr.lastIndexOf(a));//0 1 

note:searching Object item,you can't do like this:

 var a = { 1: 1, 4: 4, 5: 5 };
 arr = [{ 1: 1, 4: 4, 5: 5 }, { 1: 1, 4: 4, 5: 5 }, { 1: 1, 4: 4, 5: 55 }, { 1: 1, 4: 4, 5: 555}];        
console.log(arr.indexOf(a), arr.lastIndexOf(a));

or:
arr = [{ 1: 1, 4: 4, 5: 5 }, { 1: 1, 4: 4, 5: 5 }, { 1: 1, 4: 4, 5: 55 }, { 1: 1, 4: 4, 5: 555}];
console.log(arr.indexOf({ 1: 1, 4: 4, 5: 5 }), arr.lastIndexOf({ 1: 1, 4: 4, 5: 5 }));

because, the Object "{ 1: 1, 4: 4, 5: 5 }" is unequal to "{ 1: 1, 4: 4, 5: 5 }", every "{}" Object is the new instance Object ,they pointer represent address is difference;

let's inprove it:

console.log({ 1: 1, 4: 4, 5: 5} == { 1: 1, 4: 4, 5: 5 })//false;
console.log({ 1: 1, 4: 4, 5: 5} === { 1: 1, 4: 4, 5: 5 })//false;
Say Thanks
Respond