Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   Extending JavaScript Arrays - Part 2 (http://www.go4expert.com/articles/extending-javascript-arrays-part-2-t974/)

pradeep 5Jul2006 17:42

Extending JavaScript Arrays - Part 2
 
Continuation of Extending JavaScript Arrays.

Some more functions to extend the array manipulation capabilty of JavaScript.

Code: JavaScript

/* Shuffles the Array elements randomly */
 Array.prototype.shuffle=function()
 {
     var i=this.length,j,t;
     while(i--)
     {
         j=Math.floor((i+1)*Math.random());
         t=arr[i];
         arr[i]=arr[j];
         arr[j]=t;
     }
 }
 
 /* Removes redundant elements from the array */
 Array.prototype.unique=function()
 {
     var a=[],i;
     this.sort();
     for(i=0;i<this.length;i++)
     {
         if(this[i]!==this[i+1])
         {
             a[a.length]=this[i];
         }
     }
     return a;
 }
 
 /* Returns the index of the element matched from the behind */
 Array.prototype.lastIndexOf=function(n)
 {
     var i=this.length;
     while(i--)
     {
         if(this[i]===n)
         {
             return i;
         }
     }
     return -1;
 }



All times are GMT +5.5. The time now is 02:30.