0

I've an array with some properties. When I try to find an object's index I'm getting error TypeError: Error #1009. I'm using Flash CS6.

Here's my array:

var squareArr:Object = { a1: { piece: wr1_txt, pieceLoc: { x: "-3", y: "347" } }, b1: { piece: wn1_txt, pieceLoc: { x: "47", y: "347" } }, ... 

Below code is giving me error #1009 while I want it to return a1:

trace (squareArr.indexOf(wr1_txt)); 

1 Answer 1

2

You can't indexOf() of an Object, you should instead do a property lookup cycle.

var s:String; var found:Boolean=false; for (s in squareArr) { if (squareArr[s].piece == wr1_txt) { // YAY found it found=true; break; } } if (found) trace(s); else trace("Not found!"); 

And the s will be your index.

Of course, don't forget to parse for errors, if your target is not found, you should not say the last index is what contais target.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the answer and sorry for my english. I couldn't understand "if your target is not found, you should not say the last index is what contais target". trace(s) outputs nothing.
Hmm, then declare the var not in the cycle but before it, so that the variable will still be accessible after cycling.
but now says Not found! trace(wr1_txt); // outputs [object TextField]
ActionScript 3.0 uses variable hoisting, so it doesn't matter if he declares a var inside or outside of a for loop; it still compiles as though it's declared at the beginning of the function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.