De-I > for..in

for..in

Syntax

for(variableiterant in object){
statement; 
}

Arguments

variableiterant The name of a variable to act as the iterant, referencing each property of an object or element in an array.

object The name of an object to be iterated over.

statement A statement to execute for each iteration.

Description

Action; loops through the properties of an object or element in an array, and executes the statement for each property of an object.

Some properties can not be enumerated by the for or for..in actions. For example, the built-in methods of the Array object (Array.sort and Array.reverse) are not included in the enumeration of an Array object, and movie clip properties such as _x and _y are not enumerated.

The for...in construct iterates over properties of objects in the iterated object's prototype chain. If the child's prototype is parent, iterating over the properties of the child with for...in, will also iterate over the properties of parent.

Player

Flash 5 or later.

Example

The following is an example of using for..in to iterate over the properties of an object:

myObject = { name:'Tara', age:27, city:'San Francisco' };
for (name in myObject) {
	trace ("myObject." + name + " = " + myObject[name]);
}

The output of this example is as follows:

myObject.name = Tara
myObject.age = 27
myObject.city = San Francisco

The following is an example of using the typeof operator with for..in to iterate over a particular type of child:

for (name in myMovieClip) {
	if (typeof (myMovieClip[name]) = "movieclip") {
		trace ("I have a movie clip child named " + name);
	}
}

The following example enumerates the children of a movie clip and sends each to frame 2 in their respective Timelines. The RadioButtonGroup movie clip is a parent with several children, _RedRadioButton_, _GreenRadioButton_ and _BlueRadioButton.

for (var name in RadioButtonGroup) {
	RadioButtonGroup[name].gotoAndStop(2);
}