The JavaScript Array object extends the indexOf of method

  • 2020-03-30 02:52:25
  • OfStack

Background: the standard method for Array objects in JavaScript does not include an indexOf() method, which can be extended with the following code.
 
if (!Array.prototype.indexOf) { 
Array.prototype.indexOf = function(elt) { 
var len = this.length >>> 0; 
var from = Number(arguments[1]) || 0; 
from = (from < 0) ? Math.ceil(from) : Math.floor(from); 
if (from < 0) from += len; 
for (; from < len; from++) { 
if (from in this && this[from] === elt) return from; 
} 
return - 1; 
}; 
} 

Related articles: