Jeff
Jeff

Reputation: 2862

How do I use variable variable names in Actionscript 2?

I'm looping over an array named shopData and need to use values within that array to create new variables. Inside the loop, I'm trying something like this:

shop_(shopData[i].shopName).keyword = shopData[i].keyword;

but I'm having trouble with the portion in () and can't seem to find the right syntax for this.

Assuming shopData[i].shopName = "foo" I need to create a variable named:

shop_foo.keyword = value

or if shopData[i].shopName = "orange":

shop_orange.keyword = value

Is this even possible with AS2?

Upvotes: 1

Views: 1587

Answers (1)

weltraumpirat
weltraumpirat

Reputation: 22604

Yes, it's possible. You have to create a string representation of the variable name and use [ ] brackets:

this["shop_" + shopData[i].shopName].keyword = shopData[i].keyword;

All shop_... variables must either be member variables of a class instance (they are members of this in the above example) or you have to create a simple Object container:

var shops:Object = {};
shops["shop_" + shopData[i].shopName].keyword = shopData[i].keyword;

Just writing ["shop_" + shopData[i].shopName] to access a local variable will not compile.

Upvotes: 3

Related Questions