flash as3 - 如何在数组中找到对象的索引

mhe*_*ers 4 arrays flash indexing actionscript actionscript-3

如何在flash actionscript 3中找到数组中对象的索引/位置?我试图在循环中设置一个条件向上,如果一个对象的id等于current_item变量,我可以返回它在数组中的位置.

Rio*_*ams 14

这样的事情可能对你有帮助 - 这个例子返回值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
        }
Run Code Online (Sandbox Code Playgroud)

所以满足您的需求:

 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
        }
Run Code Online (Sandbox Code Playgroud)

如果您的项目不存在,则返回-1,否则将输出数组中的位置.

如果您需要更多信息,可以在此处查看有关AS3阵列的文章.