Removing Items from a Javascript Array

The most simple way to remove an item from a Javascript array is by using the "splice()" function. It not only removes the item, but maintains the index position of the newly updated array. This function allows you to define the index position of the element you want to remove, but also allows you to define the range of items to be removed as well.

Example:

var myArray = new Array();

myArray[0] = "Pizza";

myArray[1] = "Cereal";

myArray[2] = "Hot Dog";

 

To remove the "Cereal" array element, simply do the following

myArray.splice(1, 1);

 

I found out about this function by reading the following blog:

http://codepunk.hardwar.org.uk/ajs44.htm

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.