Created
May 23, 2014 15:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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