当前位置:网站首页>For in / for of / foreach loop
For in / for of / foreach loop
2022-06-11 08:46:00 【Give me a chestnut】
Catalog
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 .
边栏推荐
- Web design and website planning assignment 12 online registration form
- MATLAB R2022a 安装教程
- [cvpr2022] intensive reading of querydet papers
- Interprocess communication
- Is the result too different from the goal? With the help of target management, you can reach the target accurately!
- Supplementary provision plan codeworks round 760 (Div. 3)
- 2022 Niuke winter vacation 3
- ICML2022有意思的文章
- 不想项目失控?你需要用对项目管理工具
- GCC AVR(Atmel Studio+ AVR Studio)如何将结构体数组定义在程序存储器(flash)空间并进行读操作
猜你喜欢

AS 3744.1标准中提及ISO8191测试,两者测试一样吗?

Getting started with Zipkin

(resolved) the tqdm progress bar in the Jupiter notebook does not update and display in one line, but scrolls down to output

Introduction to database system experiment report answer Experiment 5: database single table query

leetcode - 518. Change II

剑指 Offer 40. 最小的k个数

显示器要申请BS 476-7 怎么送样?跟显示屏一样吗

Award winning survey | how Apache pulsar lived in 2022, you the final say
![[software tool] installation ffmpeg](/img/ac/4fbfb3f1a540ec181b94999d9029fb.jpg)
[software tool] installation ffmpeg

【CVPR2022】QueryDet论文精读
随机推荐
Sword finger offer 40 Minimum number of K
MySQL & Oracle database capacity query
Don't want the project out of control? You need to use project management tools
Introduction to database system experiment report answer Experiment 6: advanced query of data table
Introduction to database system experiment report answer Experiment 5: database single table query
Web design and website planning assignment 13 making video playlists
Uniapp plug-in development
Screaming Frog Log File Analyser 中文版安装教程
Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波
Web design and website planning assignment 14 add background music to the video
ActiveMQ简单教程,适合初学者,学习笔记yyds
XXL task executor calls local project
Idea annotation settings
EN45545-2 R26垂直燃烧测试介绍
leetcode - 739. 每日温度
字符设备驱动程序之异步通知机制
ICML2022有意思的文章
leetcode - 460. LFU 缓存
BFS on tree (tree breathing first search)
Codetop - sort odd ascending even descending linked list