Method 1: extract JQ (source network). This method is complete.
Array.prototype.unique = function(){ var results=this.sort() for ( var i = 1; i < results.length; i++ ) { if ( results[i] === results[ i - 1 ] ) { results.splice( i--, 1 ); } } return results;}
Method 2: complete method.
Array.prototype.unique = function(){ var newArray = [], temp = {}; for(var i = 0; i < this.length; i++){ temp[typeof(this[i])+this[i]] = this[i]; } for(var j in temp){ newArray.push(temp[j]); } return newArray; };
Method 3, incomplete.
Array.prototype.contains = function(item){ return RegExp( "\\b"+item+"\\b").test(this);};Array.prototype.unique = function(){ var returnArr = []; for ( var i = 0 , len = this.length; i < len; i++){ if( !returnArr.contains(this[i]) ) returnArr.push(this[i]); } return returnArr;}
Why do we say the first two are complete, because there is no such bug: ['1', 1] => 1.