当前位置:网站首页>[js] basic syntax - for loop

[js] basic syntax - for loop

2022-07-05 13:49:00 Weichi Begonia

Array , Map and Set The cycle of : for of / forEach
The loop of objects : for in

  1. Array of for loop – for (… of … ) / forEach / for (var i=0; i< length ; i++)

    var a = [1,2,3,4,5];
    for (var each of a){
          
    	console.log(each);
    }
    
    var a = [1,2,3,4,5];
    a.forEach(function (element){
          
    	console.log(element);
    })
    
    var a = [1,2,3,4,5];
    for (var i=0; i<a.length; i++){
          
    	console.log(a[i]);
    }
    

     Insert picture description here

  2. Object's for loop – for (… in …) {…}

    var obj1 = {
          'a':1, 'b':2, 'c':3};
    for (var key in obj1){
          
    	console.log(key, obj1[key]);
    }
    

     Insert picture description here

  3. Map and Set Of for loop – for ( … of …) / forEach()

    var m = new Map([['a', 1], ['b',2], ['c', 3]])
    for (var element of m){
          
    	console.log(element[0], element[1]);
    }
    

     Insert picture description here

    var m = new Map([['a', 1], ['b',2], ['c', 3]])
    m.forEach(function (value, key, map){
          
    	console.log(key, value);
    })
    

     Insert picture description here

    var s = new Set([1,2,3,4,5]);
    s.forEach(function (element){
          
    	console.log(element);
    })
    

     Insert picture description here

原网站

版权声明
本文为[Weichi Begonia]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140519440994.html