Extending JavaScript Arrays

Discussion in 'JavaScript and AJAX' started by pradeep, Feb 27, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    All web developers require to implement some JavaScript in their web applications.
    JavaScript arrays support a few in-built array manipulation methods like push,pop, but we might require more like unshift, shift, sort, shuffle, contains, clear and others.

    In JavaScript array being an object, we can extend it.
    Below you can find a few examples:

    Implementing a simple bubble sort:

    Code:
     Array.prototype.sort=function()
      {
          var tmp;
          for(var i=0;i<this.length;i++)
          {
              for(var j=0;j<this.length;j++)
              {
                  if(this[i]<this[j])
                  {
                      tmp = this[i];
                      this[i] = this[j];
                      this[j] = tmp;
                  }
              }
          }
      };
    Implementing ushift, which inserts an element into the beginning of the array.

    Code:
     Array.prototype.unshift=function(item)
      {
          this[this.length] = null;/* create a new last element */
          for(var i=1;i<this.length;i++)
          {
              this[i] = this[i-1]; /* shift elements upwards */
          }
          this[0] = item;
      };
    Implementing shift, which removes and returns the first element of an array.

    Code:
     Array.prototype.shift=function()
      {
          for(var i=1;i<this.length;i++)
          {
              this[i-1] = this[i] /* shift element downwards */
          }
          this.length =  this.length-1;
      };
    Implementing clear, which empties an array.

    Code:
     Array.prototype.clear=function()
      {
          this.length = 0;
      };
      
    Implementing contains, which checks where the specified elements exists in the array or not.

    Code:
    Array.prototype.contains = function (element) 
      {
              for (var i = 0; i < this.length; i++) 
           {
                  if (this[i] == element) 
              {
                          return true;
                  }
              }
              return false;
      };
          
    Continue to [thread=974]Extending JavaScript Arrays - Part 2[/thread]
     
  2. krisper

    krisper New Member

    Joined:
    Apr 6, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    programmer / project manager
    I know this is an old post, but since I found it anyone else might as well, so I'll just correct some of the mistakes I noticed.

    Both the shift and unshift methods are flawed.
    If we'd do the unshift method as shown here, we'd get the following:
    say we want to add 1 to the array [2,3,4,5].
    As the method suggests, we start with i=1 -> we copy number 2 from position 0 to 1;
    next iteration i=2 -> there is number 2 on position 1 from last iteration, so we copy number 2 from position 1 to 2;
    by now you already get the picture - in each iteration we copy the number 2 (or whatever you had on position 0 at the beginning) one position higher. After adding number 1 on the 0th position, we get the final array [1,2,2,2,2], which is now what we wanted.
    here is a possible fix to that:

    Array.prototype.unshift=function(item)
    {
    this[this.length] = null;/* create a new last element */
    for(var i=this.length-1;i>0;i--)
    {
    this = this[i-1]; /* shift elements upwards */
    }
    this[0] = item;
    };

    This will start coping items from the higher end and thus keeping the array and making room for the new first element.


    As for the shift method - it doesn't return anything, although the description said it would return the first element of an array. Solution:

    Array.prototype.shift=function()
    {
    var t = this[0];
    for(var i=1;i<this.length;i++)
    {
    this[i-1] = this /* shift element downwards */
    }
    this.length = this.length-1;
    return t;
    };

    I anyone notices anything wrong with my fixes, go ahead and correct me - after all we're here to do thing the right way :)
     
  3. mukeshsoftona

    mukeshsoftona Banned

    Joined:
    Oct 28, 2011
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    0
    Array of Integers



    Code:
    lass ArrayDemo {
         public static void main(String[] args) {
              int[] anArray;              // declares an array of integers
    
              anArray = new int[10];      // allocates memory for 10 integers
                
              anArray[0] = 100; // initialize first element
              anArray[1] = 200; // initialize second element
              anArray[2] = 300; // etc.
              anArray[3] = 400;
              anArray[4] = 500;
              anArray[5] = 600;
              anArray[6] = 700;
              anArray[7] = 800;
              anArray[8] = 900;
              anArray[9] = 1000;
    
              System.out.println("Element at index 0: " + anArray[0]);
              System.out.println("Element at index 1: " + anArray[1]);
              System.out.println("Element at index 2: " + anArray[2]);
              System.out.println("Element at index 3: " + anArray[3]);
              System.out.println("Element at index 4: " + anArray[4]);
              System.out.println("Element at index 5: " + anArray[5]);
              System.out.println("Element at index 6: " + anArray[6]);
              System.out.println("Element at index 7: " + anArray[7]);
              System.out.println("Element at index 8: " + anArray[8]);
              System.out.println("Element at index 9: " + anArray[9]);
         }
    } 
    The output from this program is:

    Element at index 0: 100
    Element at index 1: 200
    Element at index 2: 300
    Element at index 3: 400
    Element at index 4: 500
    Element at index 5: 600
    Element at index 6: 700
    Element at index 7: 800
    Element at index 8: 900
    Element at index 9: 1000
     
    Last edited by a moderator: Nov 7, 2011
  4. krisper

    krisper New Member

    Joined:
    Apr 6, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    programmer / project manager
    @mukeshsoftona How exactly is your post related to the previous messages?
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Infraction given.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice