[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

in as3 how do i get the key from my Array

Let's say i have this code:

var aTest:Array = new Array();
aTest["key 1"] = "value 1";
aTest["key 2"] = "value 2";

I can iterate through the array with a for each loop and get my values. This is fine.
Also, I can get the values if I know the keys but how do i get the key (ie: "key 2") from inside my for each loop?

With Thanks
0
jasonb2
Asked:
jasonb2
  • 3
  • 3
1 Solution
 
CyanBlueCommented:
Basically you cannot with for each loop.  You do need to use for in loop instead.  for in loop loops through the array through the keys whereas for each loop loops through the array through the value.  Please check the output from the code example below.

CyanBlue
var aTest:Array = new Array();
aTest["key 1"] = "value 1";
aTest["key 2"] = "value 2";

for each (var value:String in aTest)
{
	trace("noKey" + " = " + value);
	
	// Output:
	//		noKey = value 2
	//		noKey = value 1
}

trace("-----");

for (var key:String in aTest)
{
	trace(key + " = " + aTest[key]);
	
	// Output:
	//		key 2 = value 2
	//		key 1 = value 1
}

Open in new window

0
 
dgofmanCommented:
All possible solution taking by @CyanBlue – how come you have over 1M points in Adobe Flash Zone but behind me from Zone Alert list????
0
 
CyanBlueCommented:
Um, I don't even know what you meant by zone alert list but it probably is the case where I haven't been in EE for over a year except several times a year when I had to make up 3000 points to keep my status.  In other words, I've been busy that I did not visit EE quite a while.  Does that answer your question, dgofman?  ;)

CyanBlue
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dgofmanCommented:
But anyway nice to meet and welcome back. I am busy sometime and I need help
0
 
CyanBlueCommented:
I see.  I haven't seen many new posts since you are taking care of them mostly, but yeah, I will try to come back more often.  ;)

CyanBlue
0
 
jasonb2Author Commented:
thanks mate :)
it works perfectly
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now