flash as3 - how do I find an object's index in an array

32,112

Something like this might help you - this example returns the position of the value 7:

private var _testArray:Array = new Array(5, 6, 7, 8, 9, 8, 7, 6);

        public function ArrayTest() 
        {   
            trace (_testArray.indexOf(7));
            //Should output 2
        }

so for your needs:

 item variableToLookFor = 9 // Your variable here

 private var _testArray:Array = new Array(5, 6, 7, 8, 9, 8, 7, 6);

        public function ArrayTest() 
        {
            trace (_testArray.indexOf(variableToLookFor));
            //Should output 4
        }

This will return a -1 if your item doesn't exist, otherwise it will output the position in the array.

If you need more information you can check here for an article on AS3 Arrays.

Share:
32,112
mheavers
Author by

mheavers

Updated on July 09, 2022

Comments

  • mheavers
    mheavers almost 2 years

    how do you find an object's index / position within an array in flash actionscript 3? I am trying to set a conditional up in a loop where, if an object's id is equal to the current_item variable, I can return its position within the array.

  • Aditya P
    Aditya P over 12 years
    If this is a large array of say 8k elements which would be the fastest and efficient way to get the index?