flash as3 - 如何在数组中找到对象的索引 [英] flash as3 - how do I find an object's index in an array

查看:29
本文介绍了flash as3 - 如何在数组中找到对象的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

这样的事情可能对你有帮助 - 这个例子返回值 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.

这篇关于flash as3 - 如何在数组中找到对象的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆