如何在flash actionscript 3中找到数组中对象的索引/位置?我试图在循环中设置条件向上,如果对象的id等于current_item变量,我可以返回它在数组中的位置。
答案 0 :(得分: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
}
以满足您的需求:
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,否则它将输出数组中的位置。
如果您需要更多信息,可以查看here以获取有关AS3阵列的文章。