Javascript Array lastIndexOf(searchElement)

Description

Javascript Array lastIndexOf(searchElement)


// Array.prototype.lastIndexOf
Array.prototype.lastIndexOf = function lastIndexOf(searchElement) {
  for (var array = this, index = array.length - 1; index > -1; --index) {
    if (array[index] === searchElement) {
      return index;
    }/*w  w  w.  j a  va2s  . c om*/
  }

  return -1;
};

Javascript Array lastIndexOf(searchElement)

Array.prototype.lastIndexOf = function lastIndexOf(searchElement) {
 if (this === undefined || this === null) {
  throw new TypeError(this + 'is not an object');
 }

 var//from  w w w. jav a2 s.  c om
 arraylike = this instanceof String ? this.split('') : this,
 length = Math.max(Math.min(arraylike.length, 9007199254740991), 0) || 0,
 index = Number(arguments[1]) || 0;

 index = 1 in arguments ? (index < 0 ? Math.max(length + index, 0) : index) + 1 : length;

 while (--index >= 0) {
  if (index in arraylike && arraylike[index] === searchElement) {
   return index;
  }
 }

 return -1;
};



PreviousNext

Related