Example Analysis of JS for... in traversal statement usage, for... in instance analysis
This article describes the usage of JS for... in traversal statements. We will share this with you for your reference. The details are as follows:
The for... in statement is used to perform cyclic operations on the attributes of an array or object.
For (variable in object)
{
Run the code here
}
The "variable" is used to specify the variable. The specified variable can be an array element or an object attribute.
For example:
<! DOCTYPE html> <meta charset = "UTF-8"> <script> var x; var zoon = new Array (); zoon [0] = "cat "; zoon [1] = "dog"; zoon [2] = "pig"; for (x in zoon) {console. log (zoon [x]);} var student = {}; student. name = "James"; student ["age"] = 20; for (var I in student) // The previous variable I is the property name of the object {console. log (I); // name age console. log (student [I]); // Zhang San 20} </script>
Run the following command: