Last active
December 16, 2015 15:09
-
-
Save mdread/5454308 to your computer and use it in GitHub Desktop.
javascript array.contains and array.diff
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.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