Last active
December 19, 2015 21:29
-
-
Save zykadelic/6020179 to your computer and use it in GitHub Desktop.
See if the array contains the value, returns a boolean. Supports a detection between similar objects, but then requires the equalObjects function (https://gist.github.com/zykadelic/6020024).
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.includes){ | |
Array.prototype.includes = function(value, detectSimilarObjects){ | |
if(typeof detectSimilarObjects === 'undefined' || typeof value !== 'object'){ | |
detectSimilarObjects = false; | |
} | |
if(detectSimilarObjects === true && typeof equalObjects !== 'function'){ | |
throw Error("Array.includes() does not recognize equalObjects(). Get it here: https://gist.github.com/zykadelic/6020024"); | |
} | |
for(i = 0; i < this.length; i++){ | |
if(detectSimilarObjects ? equalObjects(this[i], value) : this[i] === value){ | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment