JavaScript supports different types of loops:
For-loop code block a certain number of times
For/in-Looping through the properties of an object
While-loops the specified code block when the specified condition is true
Do/while-also loops the specified code block when the specified condition is true
1. In operator : requires that its left-hand count is a string, or can be converted to a string, and the right-hand operand is an object or an array. Returns true if the value on the left side of the operator is a property name of the right object.
For example:
var point={x:1,y:2}; Object Direct amount
var has_x= "x" in point;//returns True
var has_z= "z" in point;//return False
var ts= "toString" in point;//return Tru E,tostring as Inheritance method
2. For/in statement: syntax,
For (variable in object)
Statement
Provides a way to traverse the properties of an object.
Cases:
For (Var prop in my_object) {
document.write ("Name: +prop+"; Value: "+my_object[prop", "<br>");
}
An array of JavaScript is a special object, so the for/in loop can enumerate the array subscript like an enumeration object property.
You can copy all the property names of an object into an array,
Cases:
var o= {x:1,y:2,z:3};
var a=new Array ();
var i=0;
For (a[i++] in O)
;//Empty statement, for initializing an array
3. In operator differs from the for/in statement , the for/in statement in the left can be a var statement declaring a variable, an element of an array or an attribute of an object that cannot be made into a string.
4. The Access property operators commonly used for arrays are "[]" instead of ".". use [] to name a property master string value, which is dynamic and can be changed at run time instead of an identifier ".".
Cases:
var stock_name= get_stock_name_from_user ()//Get stock name from user
var share= get_number_of_shares ()/Get Stock Number
Portfolio[stock_name]= share;//dynamically creates an array stock and assigns the example to the for/in loop for each stock, and
when the user enters his portfolio, the current value of
var value= 0 can be calculated;
For (the stock in portfolio) {
value +=get_share_value (stock) *portfolio[stock];
}
The stock accesses the name of each share.
Portfolio[stock] Access is the number of each stock.
For-in Cycle
Function: Traverse object properties, attribute names and attribute values are presented
var obj = {
"Key1": "Value1",
"Key2": "value2",
"Key3": "Value3"
};
function Enumakey () {for
(var key in obj) {
alert (key);
}
}
function Enumaval () {for
(var key in obj) {
alert (Obj[key]);
}
Enumakey (obj)
//key1 key2 key3
enumaval (obj)
//value1 value2
Arrays can also be traversed in this way, but not recommended, because the order is not guaranteed, and if attributes are added to the array's prototype, this property is also traversed.
The for-in loop should be used on the traversal of a non-array object, and looping using for-in is also known as "enumerations."
Technically, you can use the For-in Loop array (because the arrays in JavaScript are also objects), but this is not recommended. Because if an array object has been enhanced by custom functionality, a logical error can occur. In addition, in for-in, the order (sequence) of the list of attributes is not guaranteed. So the best array uses the normal for loop, and the object uses the For-in loop.