Skip to content

Instantly share code, notes, and snippets.

@mdread
Last active December 16, 2015 15:09
Show Gist options
  • Save mdread/5454308 to your computer and use it in GitHub Desktop.
Save mdread/5454308 to your computer and use it in GitHub Desktop.
javascript array.contains and array.diff
Array.prototype.contains = function(val, eqFunc /* (val, arrVal) */){
eqFunc = eqFunc || function(val1, val2){return val1 == val2;};
for(var i = 0; i < this.length; i++){
if(eqFunc(val, this[i])){
return true;
}
}
return false;
};
Array.prototype.diff = function(arr2, eqFunc){
var arr1 = this;
var res = [];
for(var i = 0; i < arr1.length; i++){
if(!arr2.contains(arr1[i], eqFunc)){
res.push(arr1[i]);
}
}
return res;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment