js for loop

1.for circulation      of the most popular cycle

2.for-in loop   to traverse the object becomes, the array, the prototype chain, but the performance is poor, inefficient, for-in the order of the final output is not fixed

  for(let index in obj){

    console.log (index, obj [index]); // index key object or array subscript, objiect [index] array of values ​​or value of the object

  }

3.forEach using callback function has three parameters, the first parameter is the current value of the index array, the second parameter is the current array index value is a third array of objects, and can not be return, break the loop ends

  let arr = [1,2,3,4]

  arr.forEach((a,b,c)=>{

    console.log (a, b, c) // a output 1234, b output 0123, c output arr [1,2,3,4]

  })

4.for-of   is es6 new cycle for writing, and for in similar format, but for-of output values directly, while supporting break, continue, return, throw loop ends, support the string loop

  for(let data of arr){

 

    console.log (data); // for-of directly output value

 

  }

 

Guess you like

Origin www.cnblogs.com/wayaoyao/p/10993172.html