Morgan
Morgan

Reputation: 1897

Actionscript object referencing from a string AS 3.0

Hey, I've got a question about refrencing properties from an actionscript object.

If i've got the following object named "groups"...

group1
   item1 = sampledata1 
   item2 = sampledata2
   item3 = sampledata3
group2
   item1 = sampledata4
   item2 = sampledata5
   item3 = sampledata6

I would access group1/item2 by typing "groups.group1.item2"

How would I create a method, where I can pass in the key in string form, and retrieve the data at that node. For example

groups.group1.item2 would return sampledata2

and

getItem("group1.item2"); would also return sampledata2

I think this is possible using eval(), but I believe that was removed in AS 3.0 which i'm using. Is there any other way to do this? Thanks.

Upvotes: 0

Views: 248

Answers (1)

CookieOfFortune
CookieOfFortune

Reputation: 13974

Use Objects the way you would use hashes.

You can initialize objects this way:

groups = 
{
   "group1":
   {
       "item1":sampledata1,
       "item2":sampledata2
   },
   "group2":
   {"item1":sampledata1...
   }
};

Or using brackets:

groups = new Object();
groups["group1"] = new Object();
groups["group1"]["item1"] = sampledata1;

Access is done like this:

groups["group1"]["item1"]

hope that helps.

Upvotes: 3

Related Questions