Skip to content

Instantly share code, notes, and snippets.

@libook
Forked from r3b/complement.js
Last active August 29, 2015 14:17
Show Gist options
  • Save libook/50e037f677daece8dade to your computer and use it in GitHub Desktop.
Save libook/50e037f677daece8dade to your computer and use it in GitHub Desktop.
// 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