for...in statements

Source: Internet
Author: User
Tags cairo

JavaScript for...in Statements

The for...in statement is used to loop an array or an object's properties.

The code for the for ... in loop executes once for each time, and the elements of the array or the properties of the object are manipulated once.

Grammar:
forinObject) {    execute code here}

A variable is used to specify a variable, which can be an array element or a property of an object.

Instance:

Iterate through the array using the for ... in loop.

for (x in mycars){document.write(mycars[x] + "<br />")} </script></body>
executes one or more statements for each property of an object, or for each element in an array or collection. for ([var] variable in {object | array | collection})
   Statement
actual Parameters
Variable

Must be selected. A variable that can be any property name of object , any index ofarray , or any element of collection.

Object

The JScript object to loop on.

Array

The array to loop on. it can be either a JScript array object or an array of. NET Framework.

Collection

The collection to loop on. it can be any class that implements the IEnumerable or IEnumerator interface from the. NET Framework.

Statement

Optional. the statement to be executed for each property of object or for each element of array or collection. can be a compound statement.

Notes

collection. " &NBSP for each cycle of the loop, variable   Assign  , object  's Next property name, array  Next index or  collection .   you can use &NBSP; variable   for any statement in the loop to refer to  , object   Properties or  , and array   elements.

When looping over an object, there is no way to determine or control the order in which the object's member names are assigned to variable. The for...in statement cannot loop on a member of a non-JScript object, for example,. NET Framework objects.

The array loops by element order, starting with the smallest index and ending with the largest index.for...in statement accesses only the defined Eleme NTS of the array. " Because jscript  Array   object may be sparse, so   for...in  Statement only accesses elements that are already defined in the array.  jscript  Array   objects can also have a expando property, in which case it is   variable   assigns an array index as the property name.   If the array is a multidimensional. NET Framework Array, only the first dimension is enumerated.

To cycle through the collection, the elements are assigned to variablein the order in which they appear in the collection.

Example 1The following example illustrates the use of a for ... in statement that uses an object as a related array.
function ForInDemo1 () {    //Initialize the object.    var obj = {"A": "Athens", "B": "Belgrade", "C": "Cairo"}    //Iterate over the properties and create the string resu Lt.    var s = "" for    (var key in obj)    {        S + = key + ":  " + obj[key];        s + = "\ n";    }    return (s);}

This function returns a string that contains the following:

A:  ATHENSB:  belgradec:  Cairo

for...in statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.