Created
July 5, 2011 23:40
-
-
Save donavon/1066223 to your computer and use it in GitHub Desktop.
Does a JavaScript array contain a search element
This file contains hidden or 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
// Array.contains | |
// compares searchElement to elements of the Array using strict equality | |
// (the same method used by the ===, or triple-equals, operator) and returns true or false | |
// Requires Array.indexOf. If not, see: | |
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf | |
// ex: [1,2,3].contains(2) returns true | |
if (!Array.prototype.contains) { | |
Array.prototype.contains = function (searchElement) { | |
return this.indexOf(searchElement) !== -1; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment