1

How can I access the member variable of an object by using a variable in the name.

Example:

Entries Object has properties 1, 2, 3, 4, 5. Normally I would access them by

var i : int = Entries.1;
var i : int = Entries.2;

However, if I have a loop

for (var j : int = 1; j < 6; j++){
  trace(Entries[j]);
}

does not work.

 Entries.(j)
 Entries.j

neither.

What's the way to go?

Entries.hasOwnProperty("j")

also does not work to check if the member exists.

Thanks!

2
  • How are you creating the Entries object? In my testing, I can access numerically named properties just fine using Entries[j]. Commented Aug 17, 2009 at 21:04
  • without converting them to a string? The Entries object comes from the backend via ZendAMF. It gets mapped to a Standard Object in Flex with a couple of members. In debug mode I can see them with [1], [2], [3] as members of the object. Commented Aug 18, 2009 at 8:13

1 Answer 1

3
Entries.hasOwnProperty("j") 

does not work because you're sending it "j" as a string, you need to convert the integer variable j to a string, therefore representing the number you are looking for. Eg:

Entries.hasOwnProperty(j.toString());

So to extract the property from your object, you can do:

for(var j:int = 1; j < 6; j++)
{
    trace(Entries[j.toString()]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I did not that I needed to convert it to a String. Works perfectly now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.