Removing Array Elements


Just like adding elements, removing them has several different options. We will start with the simple options and then move into using the array methods.

The first option for removing elements from an array is using the delete operator.

The delete Operator

The delete operator is misleading. It does not actually delete the element in the array; it merely sets the element to undefined. To use this operator, type delete then use a space to separate the array element you want to "delete" by using its index. Here's an example:

 var my_array:Array = new Array("fName","lName"); trace(my_array [0]); delete my_array [0]; trace(my_array [0]); //output: fName //        undefined 

As you can see, when we traced the first element in my_array before we used the delete operator, it displayed fName. Then after we used the delete operator, the output of the first element became undefined. Also note that the length of an array after the use of the delete operator will stay the sameeven though the operator removes the data in the element, it does not remove the element itself.

The delete operator can also be used on named array elements, as shown here:

 var my_array:Array = new Array(); my_array.fName = "David"; trace(my_array.fName); delete my_array.fName; trace(my_array.fName); //output: David //        undefined 

Just like indexed array elements, the delete operator simply removes the value of the named element, but the element is still in the array.

To remove the element itself, we have a few choices. The first involves using the length property. Then there are the pop, shift, and splice methods.

Removing Elements Using the length Property

Using the length property to remove elements in an array is very similar to using it to add elements. Just create an array and set its length, like so:

 var my_array:Array = new Array("fName","lName","age","location"); trace(my_array); my_array.length = 2; trace(my_array); //output: fName, lName, age, location //        fName, lName 

Using the length property to remove elements is a very simple way to get rid of everything that comes after the desired length of the array.

The splice Method Revisited

The splice method was already covered earlier in this chapter. This time, however, we'll use it strictly for the removal of elements in an array.

You can use the splice method in two different ways when removing elements. The first way removes all elements beginning with the starting index you define. The second way sets the number of elements to remove at the starting index. Here's an example:

 var my_array:Array = new Array ("fName","lName","age","location","phone","fax","email"); trace(my_array); my_array.splice(5); trace(my_array); my_array.splice(2,2); trace(my_array); //output: fName, lName, age, location, phone, fax, email //           fName,lName, age, location, phone //           fName, lName, phone 

The first splice sets the starting index and removes all elements at and beyond that point. The second splice sets the starting index and the number of elements to remove and then actually removes those elements. Another method used for removing array elements is the pop method.

The pop Method

The pop method can be thought of as being the "archenemy" of the push method. Whereas the push method adds elements to the end of an array, the pop method removes singular elements from the end of the array. Its syntax is the same as the other methodsjust attach the method to the array you want to remove elements from, as shown here:

 var my_array:Array = new Array("fName","lName","age","location"); my_array.pop(); trace(my_array); //output: fName, lName, age 

In this example, the pop method simply dropped the last element in the array completely and changed the length of the array.

The pop method can also return the value of the element it removes. Here's an example:

 var my_array:Array = new Array("fName","lName","age","location"); trace(my_array.pop()); //output: location 

The next method for removing array elements is the shift method.

The shift Method

If the pop method is the archenemy of the push method, then the shift method is the archenemy of the unshift method. The shift method removes one element from the beginning of an array and decreases its length by one:

 var my_array:Array = new Array("fName","lName","age","location"); my_array.shift(); trace(my_array); //output: lName, age, location 

Also like the pop method, the shift method returns the value of the element it removes:

 var my_array:Array = new Array("fName","lName","age","location"); trace(my_array.shift()); //output: fName 

But what if we don't want to get rid of the elements in an array and instead just want to change them?




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net