Working with array item in JavaScript
Posted on 9/5/2007 11:14:20 PM
in #ASP.NET 1.X
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
|