Here you can find the source of complement()
Array.prototype.each = function(fn){ fn = fn || Function.K; //from w w w. j a va2s . com var a = []; var args = Array.prototype.slice.call(arguments, 1); for(var i = 0; i < this.length; i++){ var res = fn.apply(this,[this[i],i].concat(args)); if(res != null) a.push(res); } return a; }; Array.prototype.uniquelize = function(){ var ra = new Array(); for(var i = 0; i < this.length; i ++){ if(!ra.contains(this[i])){ ra.push(this[i]); } } return ra; }; Array.complement = function(a, b){ return Array.minus(Array.union(a, b),Array.intersect(a, b)); }; Array.intersect = function(a, b){ return a.uniquelize().each(function(o){return b.contains(o) ? o : null}); }; Array.minus = function(a, b){ return a.uniquelize().each(function(o){return b.contains(o) ? null : o}); }; Array.union = function(a, b){ return a.concat(b).uniquelize(); }; Array.prototype.contains = function (obj) { var i = this.length; while (i--) { if (this[i] === obj) { return true; } } return false; };
Array.complement = function(a, b){ return Array.minus(Array.union(a, b),Array.intersect(a, b)); };
Array.prototype.complement = function(b) { return a.minus(a.union(b), a.intersect(b)); };