1.
1 Array.prototype.unique = Array.prototype.unique || function (){ 2 var arr = []; 3 for (var i = 0,len = this.length; i < len; i++){ 4 if(arr.indexOf(this[i]) == -1){ 5 arr.push(this[i]); 6 } 7 } 8 return arr; 9 };
2.
1 Array.prototype.unique = Array.prototype.unique || function (){ 2 var arr = [this[0]]; 3 for(var i = 1,len = this.length; i < len; i++){ 4 if(this.indexOf(this[i]) == i){ 5 arr.push(this[i]); 6 } 7 } 8 return arr; 9 }; 10 11 console.log([1,2,3,5,2,3].unique()); //[1,2,3,5]
3.
1 Array.prototype.unique = Array.prototype.unique || function (){ 2 var n = {}, 3 arr = []; 4 for(var i = 0,len = this.length; i < len; i++){ 5 if(!n[this[i]]){ 6 n[this[i]] = true; 7 arr.push(this[i]); 8 } 9 } 10 return arr; 11 }; 12 13 console.log([1,2,3,5,2,3].unique()); //[1,2,3,5]