Posts tagged "ActionScript"

ActionScript Tip: Splicing and Slicing

Array.splice() and Array.slice() are 2 powerful functions that you can use to manipulate the content of your List component. If used effectively, you can achieve a List filtering effect without using any other complex filtering techniques.

First off, what happens when you do this:

var myArray1:Array = new Array();
var myArray2:Array = new Array();

//Push an object to Array1
myArray1.push(new String("ALL"));

//Assign Array1 content to Array2
myArray2 = myArray1;

//Push another object to Array1
myArray1.push(new String("Only for Array1"));


Whatever objects you add to myArray1, automatically gets into myArray2 because of the Array assignment. In your application if you want the 2 arrays two share the content initially but not bound together at a later stage, you need to ‘slice’ the Array:

//Do not do this
myArray2 = myArray1;

//Do this to create a copy of myArray1
myArray2 = myArray1.slice(0);


Splicing, is another technique that will allow you to modify the content of the array without creating a copy of it.

//Remove the first element of myArray1 and assign the content to myArray2.
//When you modify myArray1, myArray2 will also change.

myArray2 = myArray1.splice(0,1);


You can follow these Array manipulation techniques in your application to create List components with filters, which will bind different arrays to the list at run time.

Here’s an application I built for the Android market, a simple action items tracking system using associative arrays and filters:

See also: