Skip to content

Instantly share code, notes, and snippets.

@zykadelic
Last active December 19, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zykadelic/6020179 to your computer and use it in GitHub Desktop.
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).
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