Working with array item in JavaScript

Hi,

Last day I was working with the some JavaScript and array. I was basically working with adding and deletion of the array item. In JavaScript we can add and delete Items in the array in the following way. 

               var exmapleArray = new Array("a", "b");

To add item at the end of the array we use the push method of the array object.

               exmapleArray.push("c"); 1

We can sort an array by using the sort method of the array object.

               exmapleArray.sort();

But to add a item in a sorted array at the correct position we will have use the arrays slice method. The following syntax will add the value c after a and b.

               exmapleArray.splice(2, 0, "c");

To delete a number of values from a given starting index also we use the slice method. Here we provide only 2 parameter. The starting index for delete and number of items to be deleted.

               exmapleArray.splice(1, 1);

Vikram

Share this post   Email it

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2010 Vikram Lakhotia