For each in is a handy statement to iterate over items in a collection (or object), and execute statements on those items – for example, an Object, Array, or XML. I’ll give an example of each of these…
Object
var myObject:Object = {name:"Shofferhoffer", type:"Wheat Beer", alcoholPercentage:4.5, origin:"Germany"}
for each (var item in myObject) {
trace(item);
}
outputs:
4.5
Germany
Shofferhoffer
Wheat Beer
Array
var myArray:Array = new Array("Corona", "Shofferhoffer", "Fosters");
for each (var item in myArray) {
trace(item);
}
outputs:
Corona
Shofferhoffer
Fosters
XML
var myXML:XML = <drinks><wine>merlot</wine><wine>shiraz</wine><beer>wheat beer</beer><beer>ale</beer><beer>lager</beer>;
for each (var item in myXML.beer) {
trace(item);
}
outputs:
wheat beer
ale
lager
The for each..in statement iterates only through the dynamic properties of an object, not the fixed properties. And unlike the for..in statement, the for each..in statement iterates over the values of an object’s properties, rather than the property names.
So, there’s a few good usage examples for for.. each.. in. Worth keeping in mind when working with objects!