Hi Guys,
I thought I should share this one with you.
I’ve been using ArrayCollection since I started to develop Mobile Apps in Flex.
There are some moments when you end up using Array for a weird reason.
And when you end up using an array you think Oh well! how to remove element from an Array.
Below are few things that I would like you to show
First up removing an Element
Say you have an Array as shown below
[geshi lang=”actionscript” nums=”1″ target=”_self” ]
1 |
var myArray = new Array("value1", "value2", "value3", "value4", "value5"); |
Lets remove value3 from the above Array
[geshi lang=”actionscript” nums=”1″ target=”_self” ]
1 2 3 |
myArray.splice(2,1); // myArray now looks like this // "value1", "value2", "value4", "value5" |
Lets remove all Elements that appear after value2
[geshi lang=”actionscript” nums=”1″ target=”_self” ]
1 2 3 |
myArray.splice(2); // myArray now looks like this // "value1", "value2" |
Lets now add value3 again after value1
[geshi lang=”actionscript” nums=”1″ target=”_self” ]
1 2 3 |
myArray.splice(0,0,"value3"); // myArray now looks like this // "value1","value3", "value2" |
If you would like to remove all Array Elements then you can use Splice function again on Array like this
[geshi lang=”actionscript” nums=”1″ target=”_self” ]
1 2 3 |
myArray.splice(0); // myArray now looks like this // no val |
Now that you have seen above you can write your own RemoveElementAt function that will work as per your requirement. Below is a basic function to achieve RemoveElementAt
[geshi lang=”actionscript” nums=”1″ target=”_self” ]
1 2 3 |
public function RemoveElementAt(arr:Array,i:int):Array{ return arr.slice(i,1); } |
I Hope this helps
Leave a Reply