Use of for in loops and for loops in JavaScript

Source: Internet
Author: User
Tags hasownproperty

JavaScript for...in statements

The for...in statement is used to loop an array or an object's properties.

The code for the for ... in Loop executes once for each time, and the elements of the array or the properties of the object are manipulated once.

The tip:for-in loop should be used on a non-array object's traversal, and looping using for-in is also known as an "enumeration."

Grammar:

For (variable in object) {    execute code here}

A variable is used to specify a variable, which can be an array element or a property of an object.

Example: Iterating through an array using the for ... in loop.

Application visible here: http://caibaojian.com/js-max-repeat.html

Note one: The For In loop does not arrange the output according to the subscript of the attribute. Http://www.cnblogs.com/rubylouvre/p/3396042.html

"First": "First",   "Zoo": "Zoo", "  2": "2", "  1": "1", "second": "  Second"};for (var i in obj) {console.log (i);}; Output: 1234firstzoosecond

Execute it by Chrome, put the nonnegative integer key in the order, sort the output, and then output the remainder of the definition. Due to this wonderful setting, Avalon's Ms-with object sort does not output as expected. You can only force users to not define key names as pure numbers.

Example 1: A new attribute is defined on the prototype object of the array, and no problem occurs with the For loop

function Getnewarray () {var array=[1,2,3,4,5]; Array.prototype.age=13;var result = [];for (var i=0;i<array.length;i++) {Result.push (array[i]);} Alert (Result.join ("));}

Example 2: A for-in loop is used, but the exact result of 12345 is given as expected.

function Getarraytwo () {var array=[1,2,3,4,5];var result=[];for (var i in array) {Result.push (array[i]);} Alert (Result.join ("));}

Example 3: After adding a property to the prototype, the enumeration is by default, and the last output 1234513

function Getnewarraytwo () {var array=[1,2,3,4,5]; Array.prototype.age=13;var Result=[];for (var i in array) {Result.push (array[i]);} Alert (Result.join ("));}

It is recommended that you do not perform a for-in loop for arrays, and in fact, in the high-performance JavaScript book, it is also emphasized that the for-in loop is not good because it always accesses the prototype of the object and sees whether there are attributes on the prototype, which inadvertently adds additional stress to the traversal.

Workaround:

If an object has a property of the given name, then Object.prototype.hasOwnProperty (name) returns TRUE. Returns False if the object was inherited from the prototype chain, or if there is no such property at all. The for-in loop is traversed in the current by hasOwnProperty, without having to consider its prototype properties.

function Finalarray () {var array=[1,2,3,4,5]; Array.prototype.age=13;var Result=[];for (var i in array) {if (Array.hasownproperty (i)) {Result.push (array[i]);}} Alert (Result.join ("));}

Precautions:

There are some browsers, such as the earlier Safari browser, which do not support this method

Use of for in loops and for loops in JavaScript

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.