Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the for in statement in javascript

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points about how to use the for in sentence in javascript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

In javascript, the "for in" statement is a special form of the for statement, which is used to iterate through the properties of an array or object, that is, to loop through the properties of an array or object; the syntax format is "for (variable in object) {execute code here}".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

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

For... Each time the code in the in loop executes, it operates on the elements of the array or on the properties of the object.

Syntax:

For (variable in object) {statement}

Variables are used to specify variables, which can be array elements or attributes of objects; variables can be preceded by a var statement to declare the variable name directly. In is followed by an expression of an object or array type. In the process of traversing an object or array, assign each value you take to a variable.

The statement statement is then executed, where you can access variable to read the value of each object attribute or array element. After execution, return to enumerate the next element, and so on until all elements are enumerated.

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

Example 1

The following example uses for... The in statement iterates through the array and enumerates each element and its value.

Var a = [1 true, "0", [false], {}]; / / declare and initialize the array variable for (var n in a) {/ / traversal array [xss_clean] ("a [" + n + "] =" + a [n] + "

"); / / display each element and its value}

Example 2

In the following example, an object o is defined and three properties are set. Then use for/in to iterate over the object properties, registering each property value in an array.

Var o = {x: 1 in y: true,z: "true"}, / / defines an object containing three attributes a = [], / / temporarily registers an array n = 0; / / defines a loop variable, initializing it to object (a [natively +] in o); / / traverses the object o and assigns all attributes to the array.

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

These are all the contents of the article "how to use for in sentences in javascript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report