r3b
4/24/2014 - 3:31 PM

Find the complement of two arrays

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;