当前位置:网站首页>For in / for of / foreach loop

For in / for of / foreach loop

2022-06-11 08:46:00 Give me a chestnut

1. for loop

for The biggest disadvantage of loops is that they need to track counters and exit conditions .
although for Loops do have an advantage in looping arrays , But some data structures are not arrays , Therefore, it is not always suitable for use loop loop .

const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let i = 0; i < digits.length; i++) {
    
  console.log(digits[i]);
}

2. for…in loop

Still need to use index To access the values of an array
When you need to add additional methods to the array ( Or another object ) when ,for…in The cycle can cause a lot of trouble . because for…in Loop through all enumerable properties , This means that if you add any other attributes to the prototype of the array , These properties also appear in the loop .

const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (const index in digits) {
    
  console.log(digits[index]);
}

3. forEach loop

forEach loop It's another form of JavaScript loop . however ,forEach() It's actually an array method , So it can only be used in arrays . Can't stop or quit forEach loop . If you want this behavior in your cycle , You need to use the basic for loop .

4. for…of loop

for…of The loop is the latest addition to JavaScript Cycle in cycle series .
It combines the form of its brother cycle for Circulation and for…in The advantages of recycling , You can loop any iteration ( That is, to abide by the iterative protocol ) Data of type . By default , Contains the following data types :String、Array、Map and Set, Note that does not include Object data type ( namely {}), By default , Objects cannot be iterated

  • for…of Loops are used to iterate over any data type that can be iterated
  • for…of How to write the loop and for…in The cycle is basically the same , Just to in Replace with of, Index can be ignored
  • for…of The cycle solved for and for…in The inadequacies of the cycle , You can stop or quit at any time for…of loop
  • Don't worry about adding new properties to the object .for…of Loop will only loop through the values in the object .
const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (const digit of digits) {
    
  console.log(digit);
}

5. const stay for in perhaps for of The use of

const Declared variables cannot be reassigned in a block level scope .

  • for Use in circulation const Will report a mistake
  • stay for of perhaps for in There will be no error when used in

reason :for in and for of Both of them are strictly iterative statements , For each attribute value in the object , A specified statement block is executed . That is, every cycle , A block level scope will be generated to complete the behavior of each variable ;

However for A loop does not traverse the properties of an object , Each cycle is performed in the same block level scope , Use const The statement will report an error . So in for in perhaps for of among , Recommended const To ensure that the accessed attribute value will not be changed by subsequent statements .

原网站

版权声明
本文为[Give me a chestnut]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110825499508.html