Javascript Array indexOf(searchElement, fromIndex = 0)

Description

Javascript Array indexOf(searchElement, fromIndex = 0)


Array.prototype.indexOf = function(searchElement, fromIndex = 0) {
 let arr = this;/*from w  w  w  .  ja v a  2  s. co m*/
 let result = -1;
 let start = Math.max(fromIndex < 0 ? fromIndex + arr.length : fromIndex, 0);
 for (let i = start, len = arr.length; i < len; i++) {
  if (arr[i] === searchElement) {
   result = i;
   break;
  }
 }
 return result;
}



PreviousNext

Related