Advertisement
rg443

Array.prototype.unique

Jan 26th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.unique = function(a){
  2. return function(){return this.filter(a)}}(function(a,b,c){return c.indexOf(a,b+1)<0
  3. });
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  Benchmark.prototype.setup = function() {
  11.     Array.prototype.contains = function(v) {
  12.         for(var i = 0; i < this.length; i++) {
  13.             if(this[i] === v) return true;
  14.         }
  15.         return false;
  16.     };
  17.    
  18.     Array.prototype.unique = function() {
  19.         var arr = [];
  20.         for(var i = 0; i < this.length; i++) {
  21.             if(!arr.contains(this[i])) {
  22.                 arr.push(this[i]);
  23.             }
  24.         }
  25.         return arr;
  26.     }
  27.    
  28.     //if you need a 'shim':
  29.     Array.prototype.filter= Array.prototype.filter || function(fun, scope){
  30.         var T= this, A= [], i= 0, itm, L= T.length;
  31.         if(typeof fun== 'function'){
  32.             while(i<L){
  33.                 if(i in T){
  34.                     itm= T[i];
  35.                     if(fun.call(scope, itm, i, T)) A[A.length]= itm;
  36.                 }
  37.                 ++i;
  38.             }
  39.         }
  40.         return A;
  41.     }
  42.      Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
  43.             if(!i || typeof i!= 'number') i= 0;
  44.             var L= this.length;
  45.             while(i<L){
  46.                 if(this[i]=== what) return i;
  47.                 ++i;
  48.             }
  49.             return -1;
  50.         }
  51.    
  52.     var duplicates = [1,3,4,2,1,2,3,8];
  53.      
  54.     // kodono.info
  55.     Array.prototype.unique2=function(){var o={},i,l=this.length,r=[];for(i=0;i<l;i++)o[this[i]]=this[i];for(i in o)r.push(o[i]);return r}
  56.   };
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. var unique= array1.filter(function(itm, i){
  64.     return array1.indexOf(itm)== i;
  65.     // returns true for only the first instance of itm
  66. });
  67.  
  68.  
  69. //if you need a 'shim':
  70. Array.prototype.filter= Array.prototype.filter || function(fun, scope){
  71.     var T= this, A= [], i= 0, itm, L= T.length;
  72.     if(typeof fun== 'function'){
  73.         while(i<L){
  74.             if(i in T){
  75.                 itm= T[i];
  76.                 if(fun.call(scope, itm, i, T)) A[A.length]= itm;
  77.             }
  78.             ++i;
  79.         }
  80.     }
  81.     return A;
  82. }
  83.  Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
  84.         if(!i || typeof i!= 'number') i= 0;
  85.         var L= this.length;
  86.         while(i<L){
  87.             if(this[i]=== what) return i;
  88.             ++i;
  89.         }
  90.         return -1;
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement