Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Removing a specific element from a vector

Community Beginner ,
Jun 02, 2011 Jun 02, 2011

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.

TOPICS
ActionScript
4.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jun 03, 2011 Jun 03, 2011

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);

}

Translate
LEGEND ,
Jun 02, 2011 Jun 02, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2011 Jun 02, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2011 Jun 02, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2011 Jun 02, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 03, 2011 Jun 03, 2011

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2011 Jun 03, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 03, 2011 Jun 03, 2011
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines