Skip to main content
Adrien_
Known Participant
June 3, 2011
Answered

Removing a specific element from a vector

  • June 3, 2011
  • 2 replies
  • 4434 views

I am wondering how to remove a specific element from a vector.

For example, assume you have the following method:

private var element:String;

private vare Element:Vector<String>;

//This function assumes that Element has elements in it.

public function removeelement(element):Boolean

{

     for (var i:int =0; i<Element.length; i++)

     {

          if (element == String(i)

          {

               //How do I remove the element assuming it matches?

               return true;

          }

     }

     return false;

}

So from the code example above I was using Element.pop() but thats wrong -I think -as it removes the LAST element. I need to remove the element at the location of the match.

This topic has been closed for replies.
Correct answer Lee_Burrows

Um not to be rude but where do you think I got the example code I showed you from LOL.

In my code I typed in Splice and saw the documentation on how its used. I was asking you if  - IYO (in your oppinion) iof that looked right based upon the information given. I understand exsactlky what splice does...Its the first thing the index I am a bit lost on since it has to be a number i was like ok convert the matched element to a number and remove one it.


hi

you can use splice to remove elements but you need to run the loop starting with the last item (and precalc length as it will change if items are removed) to avoid errors:

var len:int = element.length;

for (var i:int=len-1; i>=0; i--)

{

     if (element == String(i))

          element.splice(i,1);

}

2 replies

Kenneth Kawamoto
Community Expert
Community Expert
June 3, 2011

Vector.splice() is good but what if if you had multiple elements to remove in the Vector? I'd use recursive function like this:

private function removeElementFromStringVector(element:String, vector:Vector.<String>):void {
            if(vector.indexOf(element) > -1){
                vector.splice(vector.indexOf(element), 1);
                removeElementFromStringVector(element, vector);
            } else {
                return;
            }
}

...so that you can remove any number of elements in any positions:

var v1:Vector.<String> = new <String>["alpha", "beta", "gamma", "beta", "delta",  "beta"];
removeElementFromStringVector("beta", v1);
trace(v1);
           
var v2:Vector.<String> = new <String>["alpha", "beta", "gamma", "delta"];
removeElementFromStringVector("beta", v2);
trace(v2);

// trace

// alpha,gamma,delta
// alpha,gamma,delta

Adrien_
Adrien_Author
Known Participant
June 3, 2011

kennethkawamoto2 wrote:

Vector.splice() is good but what if if you had multiple elements to remove in the Vector? I'd use recursive function like this:

private function removeElementFromStringVector(element:String, vector:Vector.<String>):void {
            if(vector.indexOf(element) > -1){
                vector.splice(vector.indexOf(element), 1);
                removeElementFromStringVector(element, vector);
            } else {
                return;
            }
}

...so that you can remove any number of elements in any positions:

var v1:Vector.<String> = new <String>["alpha", "beta", "gamma", "beta", "delta""beta"];
removeElementFromStringVector("beta", v1);
trace(v1);
            
var v2:Vector.<String> = new <String>["alpha", "beta", "gamma", "delta"];
removeElementFromStringVector("beta", v2);
trace(v2);
 
// trace
// alpha,gamma,delta
// alpha,gamma,delta

That is a good idea how ever in this situation youll only ever remove one item at a time based upon the item you click on in the data list

Ned Murphy
Legend
June 3, 2011

For an array you would normally use the splice() method to remove an element based on its index.

Adrien_
Adrien_Author
Known Participant
June 3, 2011

So Element.splicae (Number(element), 1, element); ?

I want to start at the index of the item found, delete 1 item and the item to delete is element (what ever element is)

??

Because in some twisted world the IDE (Flash builder) Doesnt spazz out on that yet I have not tested it oO

Ned Murphy
Legend
June 3, 2011

Try looking up the splice method in the help documentation to see how it is used.