Handling the JavaScript Array Object

Initializing an Array

There are 3 ways to initialize an array
1-
var shapes = new Array(); //Optional parameter array size
shapes[0]= "Square";
shapes[1]= "Rectangle";
shapes[2]= "Triangle";

2-
var shapes = new Array("Square","Rectangle","Triangle");
3-
var shapes = ["Square","Rectangle","Triangle"];
There is no performance impact in choosing any of these.(Whether you are using primitive or complex types)

The difference between the three ways is that; in the first way, the constructor can take a parameter with the length of the array, and the length right after initialization will be the value specified even if the array has no objects yet.

var shapes = new Array(2);
//the value of shapes.length is 2
shapes[0]= "Square";
shapes[1]= "Rectangle";
shapes[2]= "Triangle";
//The array will not raise an exception as expected
//it will just increase the length of the array
//the value of shapes.length becomes

Manipulating objects in array :

1- Push: Adds the value to the end of the array and returns the length of the array after adding the element
var letters = ["a", "b", "c"];
//array has a,b,c
letters.push("d");
//array now is a,b,c,d
letters.push("e","f");
//array now is a,b,c,d,e,f

2- Unshift: Adds the value to the beginning of the array. returns the length of the array after adding the element
var letters = ["a", "b", "c"];
//array has a,b,c
letters.unshift("d");
//array now is d,a,b,c
letters.unshift("e","f");
//array now is e,f,a,b,c,d

3- Pop: removes the last element of the array. returns the object removed.
var letters = ["a", "b", "c"];
//array has a,b,c
letters.pop();
//array now is a,b
//note that if u add any parameter to the pop method it will be ignored
//without giving any errors

4- Shift: removes the first element of the array. returns the object removed.
var letters = ["a", "b", "c"];
//array has a,b,c
letters.shift();
//array now is b,c
//note that if u add any parameter to the shift method it will be ignored
//without giving any errors

5- Splice:
takes parameters (StartIndex, DeleteCount, [Object1,…]) ; a very useful method that can do multiple things.

  • Adding object
  • Removing object(s)
  • Replacing object(s)

var letters = ["a", "b", "c"];
//array has a,b,c
letters.splice(letters.length,0,"d");
//element is added to the end of the array
//array value is a,b,c,d
letters.splice(0,0,"-1");
//element is added to the beginning of the array
//array value is -1,a,b,c,d
letters.splice(0,0,"-3","-2");
//as noticed the splice can accept multiple objects to be added
//elements are added to the beginning of the array
//array value is -3,-2,-1,a,b,c,d
letters.splice(10,0,"m");
//element is added to the beginning of the array
//array value is -1,a,b,c,d,m
//giving an out of range value will not raise errors
//it will just add the specified object to the end of the array
letters.splice(1,2);
//elements are removed from the middle of the array
//array value is -3,a,b,c,d,m
letters.splice(0,1);
//element is removed from the beginning of the array
//array value is a,b,c,d,m
letters.splice(3,2);
//element is removed from the end of the array
//array value is a,b,c
letters.splice(1,1,"x","y");
//The element "b" at position 1 is removed and
//replaced with "x","y"beginning of the array
//the array value is a,x,y,c

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: