For/in loops and usage techniques in javascript, javascriptfor

Source: Internet
Author: User

For/in loops and usage techniques in javascript, javascriptfor

JavaScript supports different types of loops:

For-a certain number of times of loop code blocks

For/in-loop traversal of Object Attributes

While-when the specified condition is true, the code block specified by the loop

Do/while-the code block specified by the loop when the specified condition is true

1. in Operator: The number of operations on the left is a string or can be converted to a string. The number of operations on the right is an object or an array. If the value on the left of the operator is an attribute name of the object on the right, true is returned.

For example:

Var point = {x: 1, y: 2}; // var has_x = "x" in point; // returns true var has_z = "z" in point; // return false var ts = "toString" in point; // return true, toString is the inheritance Method

2. for/in statement: syntax,

For (variable in object)
Statement;

Provides a method to traverse object attributes.

Example:

for(var prop in my_object) {    document.write("name:"+prop+";value:"+my_object[prop],"<br>");  }

Javascript arrays are special objects. Therefore, for/in loops can enumerate array subscript like enumeration object attributes.

You can copy all attribute names of an object to an array,

Example:

Var o = {x: 1, y: 2, z: 3}; var a = new Array (); var I = 0; for (a [I ++] in o); // null statement, used to initialize an array

3. The in operator is different from the for/in statement.The left side of the for/in statement in can be the var statement for declaring a variable, an element of the array or an attribute of the object, and cannot be used as a string.

4. the commonly used access attribute operators for arrays are "[]" instead of "."."[]" Is used to name the character string value of an attribute famous teacher. It is dynamic and can be changed at runtime, rather than an identifier ".".

Example:

Var stock_name = get_stock_name_from_user (); // obtain the stock name from the user var share = get_number_of_shares (); // obtain the stock quantity portfolio [stock_name] = share; // dynamically create an array of stocks and assign values to each stock. This example is used in a for/in loop. When a user inputs his or her portfolio, you can calculate the current total value var value = 0; for (stock in portfolio) {value + = get_share_value (stock) * portfolio [stock];}

The stock accesses the name of each stock.

Portfolio [stock] accesses the quantity of each stock.

For-in Loop

Function: traverses object attributes and puts forward both attribute names and attribute values.

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 key3EnumaVal(obj)//value1 value2 value3

The Array can also be traversed in this way, but it is not recommended because the order cannot be guaranteed. If an attribute is added to the Array prototype, this attribute will also be traversed.

The for-in loop should be used to traverse non-array objects. the for-in loop is also called "enumeration ".

Technically, you can use a for-in loop array (because arrays in JavaScript are also objects), but this is not recommended. Because logical errors may occur if the array object has been enhanced by custom functions. In addition, in for-in, the Order (sequence) of the attribute list cannot be guaranteed. Therefore, it is best to use a normal for loop for arrays and a for-in loop for objects.

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.