Go4Expert

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

pradeep 27Feb2006 19:11

Extending JavaScript Arrays
 
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: javascript

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: javascript

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: javascript

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

Implementing clear, which empties an array.

Code: javascript

Array.prototype.clear=function()
  {
      this.length = 0;
  };

Implementing contains, which checks where the specified elements exists in the array or not.

Code: javascript

Array.prototype.contains = function (element)
  {
          for (var i = 0; i < this.length; i++)
       {
              if (this[i] == element)
          {
                      return true;
              }
          }
          return false;
  };

Continue to Extending JavaScript Arrays - Part 2

krisper 6Apr2011 11:52

Re: Extending JavaScript Arrays
 
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[i] = 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[i] /* 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 :)

mukeshsoftona 7Nov2011 13:41

Re: Extending JavaScript Arrays
 
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

krisper 7Nov2011 17:50

Re: Extending JavaScript Arrays
 
@mukeshsoftona How exactly is your post related to the previous messages?

shabbir 7Nov2011 19:35

Re: Extending JavaScript Arrays
 
Quote:

Originally Posted by krisper (Post 88897)
@mukeshsoftona How exactly is your post related to the previous messages?

Infraction given.


All times are GMT +5.5. The time now is 17:25.