2010-06-09 56 views
1
var array1:Array = new Array(); 
var array2:Array = new Array(); 

var obj1:Object = new Object(); 

array1.push(obj1); 
array2.push(obj1); 

我该怎么做 我应该使用什么命令?在actionscript中查找对象在数组中的位置

//obj1's position in array1 
//result: 0 
//obj1's position in array2 
//result: 0 

回答

3

如果你想找到在阵列堆栈中的目标位置,

您可以使用indexOf

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Array.html#indexOf()

对于概念证明

array1.push(obj1); 

array1.push(obj2); 

array1.push(obj3); 

array1.push(obj4); 

array1.push(obj5); 

trace(array1.indexOf(obj5)); // Should return 4 [ 0 Ob1, 1, Obj2 ... etc] 

trace(array1.indexOf(obj1)); // Should return 0 
+0

记住如果你的数组非常大,这是一个昂贵的过程。考虑使用字典而不是数组,如果你需要做很多这种查找。 – Ender 2010-06-09 19:09:24