How to use for in statement in javascript

青灯夜游
Release: 2021-06-30 15:05:08
Original
4160 people have browsed it

In JavaScript, the "for in" statement is a special form of the for statement, which is used to traverse the properties of an array or object, that is, to perform a loop operation on the properties of an array or object; the syntax format is "for (variable in object){Execute code here}".

How to use for in statement in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The for...in statement is a special form of the for statement, used to traverse the properties of an array or object (loop through the properties of an array or object).

for ... in Each time the code in the loop is executed, an operation will be performed on the elements of the array or the properties of the object.

Syntax:

for (变量 in 对象)
{
    statement
}
Copy after login

"Variable" is used to specify a variable. The specified variable can be an array element or an attribute of an object; "Variable" can be in Append the var statement in front to directly declare the variable name. in is followed by an expression of object or array type. In the process of traversing the object or array, assign or assign each value to a "variable".

Then execute the statement statement, where the variable can be accessed to read the value of each object property or array element. After execution is completed, return and continue to enumerate the next element, and so on until all elements have been enumerated.

For arrays, the value is the subscript of the array element; for objects, the value is the property name or method name of the object.

Example 1

The following example uses the for ... in statement to traverse the array and enumerate each element and its value.

var a = [1,true,"0",[false],{}];  //声明并初始化数组变量 
for (var n in a) {  //遍历数组
    document.write("a["+n+"] = " + a[n] + "<br>");  //显示每个元素及其值
}
Copy after login

How to use for in statement in javascript

Example 2

In the following example, define an object o and set 3 properties. Then use for/in to iterate over the object properties and store each property value into an array.

var o = {x : 1,y : true,z : "true"},  //定义包含三个属性的对象
    a = [],  //临时寄存数组
    n = 0;  //定义循环变量,初始化为0
for (a[n++] in o);  //遍历对象o,然后把所有属性都赋值到数组中
Copy after login

The for (a[n] in o); statement is actually an empty loop structure, and the semicolon is an empty statement.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to use for in statement in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template