Js in a cyclic manner and various methods of traversal


for loop

  1, for three expressions: ① loop variable declaration; ② Analyzing cycle conditions; ③ update loop variable; between three expressions, with; split,

          expression for three cycles can be omitted, but the two ";" indispensable.

  2, performed for loop characteristics: first determine and then executed, while the same  

    3, for three cycles table expressions can have multiple parts, with the second portion of the plurality of determination conditions && || connection, the first three parts separated by commas;

           

for ( var num = 1; whether <10; whether ++ ) { 
               console.log (whether); // 1,2,3,4,5,6,7,8,9 
              }

 

while loop  

    Expression () in the while loop, the calculation result may be various types, but eventually into true and false, the following conversion rule.

 

  ①Boolean: true is true, false false;
  ②String: empty string is false, all non-empty string is true;
  ③Number: 0 is false, all non-zero number is true;
  ④null / Undefined / NaN: are all false;
  ⑤Object : All is true.

var NUM =. 1; // . 1, the loop variable declared 
            
the while (NUM <10) { // 2, determining the loop condition; 
    the console.log (NUM); // . 3, performing the loop operation; 
    NUM ++; // . 4, Update loop variable; 
}

do-while loop  

while loop characteristics: after performing the first determination;

do-while loop characteristics: first, then execute judgment, even if the initial condition is not satisfied, do-while loop is executed at least once, do-while loop that is executed more than once over the while loop.

 

var num = 10;
            
do{
    console.log(num);//10 9 8 7 6 5 4 3 2 1 0
    num--;
    }while(num>=0);
            
   console.log(num);//-1

 Nested loop

      The outer loop line
      The inner loop control column
      Nested loop: to solve the structure of rows and columns
    
for(var j=1;j<9;j++){
        for(var i=0;i<j+1;i++){
            document.write("*");
        }
        document.write("<br>");
    }

       

       

Traversal methods

for - in

for - in statement or an object property array for cyclic operation is performed.

for - in each code loop performed once, the properties will be an array or an object in one operation.

 

let obj={'name':'programmer','age':'22','height':'180'};
for(let i in obj){
    console.log(i,obj[i])
}

 

for - of

for ... of a range of circulating arrays may be used include, Set, and Map structures, some of array-like objects (such as arguments object, DOM NodeList objects), hereinafter the Generator objects, and character strings.

 JavaScript original for-in loop, can only obtain key subjects you can not get the key directly. ES6 provided for ... of the cycle, to allow traversal key to obtain

  Array operations:

 var arr = ['a', 'b', 'c', 'd'];

        for (let a in arr) {
          console.log(a); // 0 1 2 3
        }
        
        for (let a of arr) {
          console.log(a); // a b c d
        }

 Array-like object operations:

        // 字符串
        var str = "hello";
        
        for (let s of str) {
          console.log(s); // h e l l o
        }
        
        // DOM NodeList对象
        let paras = document.querySelectorAll("p");
        
        for (let p of paras) {
          p.classList.add("test");
        }
        
        // arguments对象
        function printArgs() {
          for (let x of arguments) {
            console.log(x);
          }
        }
        printArgs('a', 'b');// 'a' 'b'

 

  Loop control statements

     1, break: This layer out of loop, the loop continues back.
    If the loop has multiple layers, it can only break out of one.
      2, continue: skip this cycle the remainder of the code, the next cycle continues.
    ① of the for loop statement execution, then continue, a loop variable update statement i ++;
    statement ② For After while, do-while loop, continue execution, a loop condition is determined;
    therefore, the use of these two cycles must be after use put i ++ continue, otherwise, continue will skip i ++ into an infinite loop.

      

 for(var i=0;i<10;i++){
        if(i == 5){
            break;
        }
        console.log(i);//0,1,2,3,4
    }

    for(var i=0;i<10;i++){
        if(i == 5){
            continue;
        }
        console.log(i);//0,1,2,3,4,6,7,8,9
    }

 


  

 

Guess you like

Origin www.cnblogs.com/zl-light/p/11482005.html