A summary of the loop methods inside JavaScript

Source: Internet
Author: User

One, the loop in native javascript:

for Loop code block a certain number of times, it has three parameters to determine the number of cycles of the code block, the first is the initial value, the second is the terminating value, the third parameter is the change rule:

For loop for (var i = 0, len = jsonarr.length; i < Len; i++) {Console.log (Json.stringify (Jsonarr[i]));}

the For In loop is typically used to traverse the properties of an object, it has two parameters, in the previous property name parameter, in followed by the object to be traversed, it iterates through each property in the object, key is the property name, and the object "property name" outputs the property value:

for (var key in jsonobj) {Console.log (key + ': ' + Jsonobj[key]);}

Note: The key here is a string, not a real index, for-in is a loop designed for a normal object, and it can be traversed to get the key of the string type, which is inconvenient if it is used to traverse the array.

ForEach Loop

Jsonarr.foreach (function (Value,index,array) {console.log (' ******************** '); Console.log (value); Console.log (index); console.log (array);});

This is the output, as you can see, value is the value of the array element, index is the current worth of indexes, and the array is the object that is currently being traversed.

for of loops

var Jsonarr = [{' One ': ' 1 '},{': ' 2 '},{' three ': ' 3 '}];//for of the loop for (var value of Jsonarr) {Console.log (value);}

The value of each item that will output the array.

The for-of loop can iterate over the array, but it also iterates through the other collections. For example, most class array objects, such as Dom NodeList objects.

var nodelis = document.getElementById (' ul '). Childnodes;//for of the loop for (var value of Nodelis) {Console.log (value);}

The for-of loop also supports string traversal, which treats a string as a series of Unicode characters to traverse:

var str = ' I am little Sun '; for (var value of str) {Console.log (value);}

While and do While loops:

var i=0while (i<5) {console.log (i); i++;} Do{console.log (i); i++;} while (I<5)

The difference between the two is that do is executed at least once, whether the condition is satisfied or not.

Second, the jquery secondary cycle:

$.each () is used to iterate over arrays and objects:

To iterate over an array:

var arr1 = [0, 1, 2, 3, 4];var arr2 = $ (' ul Li '); $.each (arr1, function (index,value) {console.log (index) Console.log (value)} );

To traverse an object:

$.each (jsonobj, Function (key, Val) {alert (Jsonobj[key]);});

$ (). each () Iterates over the node object in jquery: $ (selector). each (function (index,element))

$ ($ (' ul Li ')). each (function (index,value) {console.log (index) Console.log (value)});

Some of the articles are mentioned in the $. (). each () is primarily used to process the DOM, and in the test it is found that both can be used for the same processing of the DOM, and there seems to be no big difference.

A summary of the loop methods inside 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.