Derek Adair
Derek Adair

Reputation: 21915

Get the "key" of an object in Actionscript-3

Given an Object:

myObj = {key : 'value'}

How do I get the key?

Upvotes: 23

Views: 24820

Answers (2)

Patrick
Patrick

Reputation: 15717

You have to loop through the all the keys

for (var key:String in myObj) {
 //...
}

Note: for(x in obj) iterates over the keys, while for each(x in obj) iterates over the values.

Upvotes: 38

Reuben
Reuben

Reputation: 1202

Use a for in loop

var myObject:Object = {key1:"value1",key2:"value2"}

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

output:

key: key1 value: value1
key: key2 value: value2

Upvotes: 11

Related Questions