且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-10-25 03:39:59

这样的事情可能对你有帮助 - 这个例子返回值 7 的位置:

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
        }

满足您的需求:

 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
        }

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

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

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

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