Summary of js Loops

Source: Internet
Author: User
Summary of js Loops
There are two types of js native loops: General for loops and for... in loops. There is also a common jQuery. each () loop. I. js native loop. for loop, the Code is as follows: var myArray = [1, 2, 3]; for (var I = 0; I <myArray. length; I ++) {console. log (myArray [I]) ;}; console: 1, 2, 3b. for... in loop, the Code is as follows: var myArray = [1, 2, 3]; for (var arr in myArray) {console. log (arr) ;}; console: 1, 2, 3c. for and... in has one thing in common: can be used for the array loop d. for and... differences of in:... in-loop can be used in addition to array loops and key loops of objects. The Code is as follows: var myObject = {"id": "1", "name ": "john"}; for (var obj in myObject) {console. log (obj) ;}; console: id, namee. control Loop statement break; jump out of this loop continue; jump out of this loop while loop: var cars = ["BMW", "Volvo", "Saab", "Ford"]; var I = 0; while (cars [I]) {console. log (cars [I] +"
"); I ++;} console: BMW Volvo Saab Forddo-while loop: var x =" "; var I = 0; do {x = x + "the number is" + I +"
"; I ++; console. log (x);} while (I <5) II. jQuery. each () loop a. The code for Traversing DOM nodes is as follows:
      
  • A
  •   
  • B
  •   
  • C
$ ("Li "). each (function () {alert ($ (this ). text ()}); B. traverse the array var arr = [1, 2, 3]; $. each (arr, function (I) {console. log (arr [I]);}); console: 1, 2, 3c. loop object var myObject = {"one": 1, "two": 2, "three": 3 };$. each (myObject, function (I) {console. log (myObject [I]) ;}); console: 1, 2, 3 d. the code for looping a two-dimensional array is as follows: var myArray = [[, 3], [, 6], [, 9]; $. each (myArray, function (I, item) {console. log (item [0]) ;}); console: 1, 4, 7e. loop Control statement: return false; jump out of this loop return true; continue to the next loop

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.