Dela via


for...in Statement

Executes one or more statements for each property of an object, or each element of an array or collection.

for ( [var] variable in {object | array | collection})
   statement

Arguments

  • variable
    Required. A variable that can be any property name of object, any index of array, or any element of collection.

  • object
    A JScript object over which to iterate.

  • array
    An array over which to iterate. Can be a JScript Array object or a .NET Framework array.

  • collection
    A collection over which to iterate. Can be any class that implements the IEnumerable or IEnumerator interfaces from the .NET Framework.

  • statement
    Optional. Statements to be executed for each property of object or each element of array or collection. Can be a compound statement.

Remarks

Before each iteration of a loop, variable is assigned the next property name of object, the next index of array, or the next element of collection. You can use variable in any of the statements inside the loop to reference the property of object or the element of array.

When iterating over an object, there is no way to determine or control the order in which the member names of the object are assigned to variable. The for...in statement cannot loop over the members of non-JScript objects, such as .NET Framework objects.

Arrays are iterated in element order, starting with the smallest index and ending with the largest index. Because JScript Array objects can be sparse, the for...in statement accesses only the defined elements of the array. JScript Array objects may also have expando properties, in which case variable is assigned array indexes as property names. If the array is a multidimensional .NET Framework array, only the first dimension is enumerated.

For iteration over a collection, the elements are assigned to variable in the order in which they appear in the collection.

Example 1

The following example illustrates the use of the for ... in statement with an object used as an associative array.

function ForInDemo1()
{
    // Initialize the object.
    var obj = {"a" : "Athens" , "b" : "Belgrade", "c" : "Cairo"}

    // Iterate over the properties and create the string result.
    var s = ""
    for (var key in obj)
    {
        s += key + ":  " + obj[key];
        s += "\n";
    }
    return (s);
}

This function returns the string that contains the following.

a:  Athens
b:  Belgrade
c:  Cairo

Example 2

This example illustrates the use of the for ... in statement with a JScript Array object that has expando properties.

function ForInDemo2()
{
    // Initialize the array.
    var arr = new Array("zero","one","two");

    // Add a few expando properties to the array.
    arr["orange"] = "fruit";
    arr["carrot"] = "vegetable";

    // Iterate over the properties and elements
    // and create the string result.
    var s = "";
    for (var key in arr)
    {
        s += key + ":  " + arr[key];
        s += "\n";
    }
    
    return (s);
}

This function returns the string that contains the following.

0:  zero
1:  one
2:  two
orange:  fruit
carrot:  vegetable

Example 3

The following example illustrates the use of the for ... in statement with a collection. Here, the GetEnumerator method of the System.String object provides a collection of the characters in the string.

function ForInDemo3()
{
    // Initialize the collection.
    var str : System.String = "Test.";
    var chars : System.CharEnumerator = str.GetEnumerator();

    // Iterate over the collection elements and
    // create the string result.
    var s = "";

    var i : int = 0;
    for (var elem in chars)
    {
        s += i + ": " + elem
        s += "\n";

        i++;
    }

    return(s);
}

This function returns the string that contains the following.

0:  T
1:  e
2:  s
3:  t
4:  .

Requirements

Version 5

Note

Looping over collections requires Version .NET.

See Also

Reference

for Statement

while Statement

String.GetEnumerator

Other Resources

JScript Arrays