For-in loop in Javascript

Source: Internet
Author: User
Tags cairo
< Script >
// Executes one or more statements for each attribute of an object, or each element in an array or set.
// For ([Var] variable in {object | array | collection })
/* Variable

Required. A variable can be any attribute name of an object, any index of an array, or any element of a collection.

*/
Function Forindemo (){
// Create some variables.
VaR A, key, S =   "" ;
// Initialize the object.
A = { " A " : " Athens " , " B " : " Belgrade " , " C " : " Cairo " }
// Iteration attribute.
For (Key In A ){
S + = A [Key] +   " <Br/> " ;
}
Return (S );
}
VaR T = Forindemo ();
Alert (t );

Function OK (AA, BB, CC ){
  This . AA = AA
  This . Bb = Bb
  This . CC = CC
}
Newemp =   New OK ( " H1 " , " H2 " , " H3 " ); // H1 is assigned to AA, H2 is assigned to BB, and H3 is assigned to CC.
Document. Write ( " Me: " + Newemp. AA + " <Br> " ); // Output AA,
Document. Write ( " You: " + Newemp. bb + " <Br> " ); // Output bb
Document. Write ( " He: " + Newemp. CC ); // CC output

// In the form of for ---- In // attribute and Value
Function OK (AA, BB, CC ){
This . AA = AA
This . Bb = Bb
This . CC = CC
}
Newemp =   New OK ( " Me: h1 <br> " , " You: H2 <br> " , " He: h3 <br> " );
For (X In Newemp) // loop an object newemp, X is the property: the property is: aa bb cc values are: Me: h1 <br> You: H2 <br> He: h3 <br>
Document. Write (newemp [x]);

// In the manual !!
// Create an object with some properties.
VaR Prop, myobject =   New Object ();
Myobject. Name =   " James " ;
Myobject. Age =   22 ;
Myobject. Phone =   " 555 1234 " ;
// Loop through all the properties in the object.
For (Prop In Myobject ){
Print ( " Myobject. "   + Prop +   " Equals "   + Myobject [prop]);
}

FunctionForindemo1 (){
VaRRET= "";

// Initialize the object with properties and values.
VaR OBJ: Object = { " A " : " Athens " ,
" B " : " Belgrade " ,
" C " : " Cairo " };

// Iterate over the properties.
For ( VaR Key In OBJ)
// Loop and assign 'A', 'B', and 'C' to key.
RET + = Key +   " : \ T "   + OBJ [Key] +   " \ N " ;

return (RET );
} /// forindemo1
/ SCRIPT>

Related Article

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.