Skip to content

Instantly share code, notes, and snippets.

@r3b
Created April 24, 2014 15:31
Show Gist options
  • Save r3b/11258949 to your computer and use it in GitHub Desktop.
Save r3b/11258949 to your computer and use it in GitHub Desktop.
Find the complement of two arrays
// returns things in array 'a' that are not in array 'b'
// > ['a','b','c','1', '2', '3'].complement(['b', 'c', 'd', 'e']);
// ['a', '1', '2', '3']
function complement(a, b){
(b)||(b=a, a=this);
return (Array.isArray(a) && Array.isArray(b))
? a.filter(function(x){return b.indexOf(x)===-1;})
: undefined;
}
Array.prototype.complement=complement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment