Five kinds of loop

 

 

/ Knowledge Point 1 ------ for in loop iterates it has two parameters i represents an index value or a key name Oh arr represent you write that array. for in the main loop that may be used in an array of objects circulating loop
  var obj = { 
        name: "Xunhuan" , 
        Age: 23 is , 
        FAV: "JS" 
    }; 

    var ARR = [11,34,55,66,77 ]; 

    for ( var I in ARR) { 
        console.log (ARR [I]); 
    } 

    for ( var I in obj) { 
        console.log (obj [I]);   // output value of the output value Xunhuan 23 js 
       console.log ( I);   // output Age FAV name 
    }

-------------------------------------------------- -------------------------------- 
knowledge point 2 ------ for   of circulation Oh. ---- it can not cyclic object item is the value of your content it also has two first parameter is a parameter representative of the value of the output item of the array arr is circulated
  for ( var item of arr) { 
         the console.log (item) 
     }
 ------------------------------------------------- ---------------------------------
 for in and for of distinction
 for in arrays and objects can be recycled.                   for of loop array of   
the first parameter is the name of the key index or the first parameter is the value of the output of   

the same point: there are two parameters       
for in stronger than for of the write
 ------------- -------------------------------------------------- --------------- 
knowledge point 3------- // forEach is not able to circulate object's value is the content index is an index array is an array you write. 
    Three parameters you can write only one. value Oh!   
forEach is no return value. 

// Description forEach no return value 
   var ARR = [10, 20 is, 30 ];
    var newArr = arr.forEach ( function (V) {
        return V 
   }); 
   the console.log (newArr); // no return value undefined 
- -------------------------------------------------- --------- 
 arr.forEach ( function (value, index, array) { 
        the console.log ( "index" + index + ",,, content" + value + "your array" + array); 
    }) 

 arr.forEach ( function (V) {// case with only one parameter. 
     the console.log (V) 
 }); 

 arr.forEach (V => the console.log (V));   // this is es6 wording 
------------------- -------------------------------------------------- ---------- 
knowledge 4 ---- the Map method returns a new array. 

var ARR = [11,34,55,66,77 ]; 

   arr.map ( function (V, I, ARR) {
     //   the console.log (I); // I is the index value starting from 0 
    //   Console .log (v) // v represents the class content 
     the console.log (ARR [I]) // output class content 
   })

 ================ "ES6 
arr.map (V => the console.log (V))
-------------------------------------------------- ------------------------------
 //     Map returns a new array 
    var Move = [ 
        {name: "San" , Score: "9.3" }, 
        {name: "John Doe", Score: ". 8" } 
        ]; 

    move.map ( function (V) { 
        v.score = parseFloat (v.score); // string variable for numeric type 
        return V; 
    }); 
    the console.log (Move);
 -------------------------------- -------------------------------------------------- - 
knowledge point 5. ------- used to filter arrays. There are new return value. arr.filter
 var ARR = [11,34,55,66,77 ];
var newarr = arr.filter ( function (V) {
     return V> 20 is ; 
}) 
the console.log (newarr); // var newarr = [34,55,66,77];
 
--------- ------------------------------------------------- 
summary : consistent usage parameter map and filter. 
The same point: are brand new array of return. There are three parameters. We are in the callback function. 
Not powered: filter used to filter. 
There are five cycles of methods for  in      for of the Map forEach filter 

Summary: Familiar js, for es6 very understanding. For example, the cycle of five new method,   for  in      for of the Map forEach filter 

for in and for of difference
 for in an array can cycle and objects, mainly for recycling objects.                   for of loop array  
The first parameter is the name of the key index or the first parameter is the value of the output of   

the same point: there are two parameters       
for in stronger than for of 

forEach loop arrays are used, it is no return value. 

Summary: consistent usage parameter map and filter. 
The same point: are brand new array of return. There are three parameters. We are in the callback function. 
Not powered: filter used to filter. 
5 cycles of methods for  in      for of filter forEach Map

 

Guess you like

Origin www.cnblogs.com/IwishIcould/p/11261439.html