Which is equivalent to foreach (with keys) in ActionScript

I am looking for the equivalent of a foreach loop with keys in ActionScript. In PHP, it will 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 a for..in loop. Which gives you the keys, but you should still 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 for each cycle ... which, in my opinion, is new in AS3. With this solution, I can’t say what the keys are. For example:

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

So far, I'm going to use for .... I'm just looking for a better way.

Thanks Rob

: , . , . :

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

, .

, . , :

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

, .

+3
2
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';

, , . .

0

. , :

DictionaryUtil.getKeys(MyObject)

... , . , . , (... ) - .

: http://www.gskinner.com/blog/archives/2006/07/as3_dictionary.html.

+2

Source: https://habr.com/ru/post/1710264/


All Articles