Working with array item in JavaScript
Posted on 9/5/2007 11:14:20 AM
in #ASP.NET 1.X
Hi,/SPAN>/P>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.
/SPAN>var exmapleArray = new Array("a", "b");/SPAN>
To add item at the end of the array we use the push method of the array object. /SPAN>/P>
/P>
/SPAN>exmapleArray.push("c"); 1/SPAN>/P>We can sort an array by using the sort method of the array object.
/SPAN>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.
/SPAN>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.
/SPAN>exmapleArray.splice(1, 1);
Vikram/SPAN>/SPAN>
|