Skip to content

Instantly share code, notes, and snippets.

@phantom42
Created May 23, 2014 15:07
Show Gist options
  • Save phantom42/79688580c67176cb1411 to your computer and use it in GitHub Desktop.
Save phantom42/79688580c67176cb1411 to your computer and use it in GitHub Desktop.
indexOf function for browsers lacking support (looking at you IE < 9). From answer on SO. http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array#144172
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (obj, fromIndex) {
if (fromIndex == null) {
fromIndex = 0;
} else if (fromIndex < 0) {
fromIndex = Math.max(0, this.length + fromIndex);
}
for (var i = fromIndex, j = this.length; i < j; i++) {
if (this[i] === obj)
return i;
}
return -1;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment