views:

780

answers:

2

I am looking for the equivalent of a foreach loop with keys in Actionscript. In PHP this would be:

foreach($array as $key => $value)
{
}

I found two solutions that will work, but I am wondering if there is a better way to do this. The first solution is to use the for..in loop. Which gives you the keys, but you still have to use the key to access the correct element in your structure. For example:

for(var key:String in results)
{
    trace(key + ": " + results[key]);
}

The second option is the for each..in loop, which I believe is new in AS3. With this solution, I can't tell what the keys are. For example:

for each(var row:* in results)
{
    trace(row);
}

For the time being, I am going to use for..in. I am just looking for a better way.

Thanks, Rob

Update: Speed is not a huge deal, because the array is never going to be extremely large. Order does matter, and I would like to get keys in the order of insertion. Here is an example of the array:

sites = {'site1': 34, 'site2': 52, 'site3': 66}

I would like to use the key as well as the value of each entry.

I want to keep my array structure as simple as possible. I could change the sites array above to look like:

sites = {{'name': 'site1', 'id': 34}, 
    {'name': 'site2', 'id': 52},
    {'name': 'site3', 'id': 66}}

However, I would rather not go this route, because it adds more dimensions to my array structure.

+2  A: 

It depends on your object type. If you're using a Dictionary, you have:

DictionaryUtil.getKeys(myObject)

I wouldn't use for...in unless you're just dumping or purely want the keys and nothing else. It is an object so sort isn't guaranteed and will vary. If sorting isn't an issue, this (for...in or dictionary) is your best bet.

Grant speaks more on dictionary here: http://www.gskinner.com/blog/archives/2006/07/as3_dictionary.html.

johncblandii
With your updated description, I believe you'll want to change your data structure. The first structure is an associative array which will not hold your insertion order. The latter is the way you need to do it. I would suggest using value objects vs {..} for your internal objects.
johncblandii
Thinking about it more, you are right insertion order will be lost if I just go the assoc array route. Could you provide an example of what you mean by using value objects compared with the {..} that I am using now?
Rob Lund
var b:MyObject = new MyObject();b.blah = "hello world";b.isTopOfTheWorld = true;b.eek = "ha";...other vo's created here...var myArray:Array = [b, ...];Note: I know this comment will looked jacked up but basically when parsing your content create a simple object with the properties. This opens you up to binding, etc and makes your code more readable/"followable" by others.
johncblandii
A: 
for(var i:String in myArray) // loops through the items in the array
    myArry[i] += 'new message will show'


for each(var i:String in myArray) // creates a copy of the array as it loops
    myArray[i] += 'this change will not show outside the loop';

The later is great for if you need to minipulate the variables during the loop but want to preserve the original object for the rest of your program. Especially handy for formatting or translating values for a specific part of your program.

Fire Crow
`for .. in` loops the var through the keys, `for each .. in` loops the var through the values. It has nothing to do with copying arrays.
Simon Buchan
my code is correct please address what you problem is with my terminology for what is going on.
Fire Crow