Javascript Array lastIndexOf(searchElement, fromIndex = this.length - 1)

Description

Javascript Array lastIndexOf(searchElement, fromIndex = this.length - 1)



Array.prototype.lastIndexOf = function(searchElement, fromIndex = this.length - 1) {
 let arr = this;/*from  w  ww . j a  v  a  2 s  .  com*/
 let result = -1;
 let start = fromIndex < 0 ? fromIndex + arr.length : fromIndex;
 for (let i = start; i >= 0; i--) {
  if (arr[i] === searchElement) {
   result = i;
   break;
  }
 }
 return result;
}



PreviousNext

Related