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:
$ ("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