1

I have an array as follow.

var array:Array = new Array();
array["Circle"] = 1;
array["Rect"] = 2;

I wantto read the values by using a variable.

var key:String = "Circle";
trace(array[key]);

Can anyone guide me how to achieve this. Its not neccessary to go with Array only. I may switch to whichever collection in which this is possible.

2
  • What is it exactly that you would like to achieve. Do you want some code to loop over all key value pairs of your Map? Commented Oct 21, 2011 at 14:39
  • Looks to me that you've already figured it out. Commented Oct 21, 2011 at 14:41

1 Answer 1

3

Use Object or Dictionary:

var obj:Object = new Object();
obj["Circle"] = 1;
obj["Rect"] = 2;

// alternative initialization - only for Object
obj = {Circle: 1, Rect: 2};

for (var key:String in obj)
{
    trace("key:", key, ",", "value:" obj[key]);
}

// output:
// key: Circle , value: 1
// key: Rect , value: 2

for each (var value:Object in obj)
{
    trace(value);
}

// output:
// 1
// 2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.