当前位置:网站首页>Difference between foreach, for... In and for... Of
Difference between foreach, for... In and for... Of
2022-06-11 06:21:00 【Geek student】
List of articles
1. forEach, for in , for of Differences among the three ?
1.1 forEach
- More concise when traversing , Efficiency and for identical , Don't worry about set subscripts , Reduce the efficiency of errors
- no return value
- Out of commission break Break the loop , Out of commission return Return to the outer loop
const array = [1, 3, 4];
let newArray = arr.forEach(i => {
i += 1;
console.log(i); //2,4,5
});
console.log(arr); //[1,3,4]
console.log(newArray); //undefined
1.2 for in
for…in Used to traverse arrays or object properties ( Most of them are object )
Traversable The key name of the array , Traversing objects is simple and convenient
// Traversing objects
let person = {
name: 'xiaosheng', age: 24, city: ' Shenzhen ' };
let text = '';
for (let i in person) {
text += person[i];
}
// The output is : zhleon24 Shenzhen
// Traversal array
let arry = [1, 2, 3, 4, 5];
for (let i in arry) {
console.log(arry[i]);
}
// The output is 1,2,3,4,5
1.3 for of
ES6 reference C++、Java、C# and Python Language , Introduced for…of loop ,
for…of Can traverse all iteratable objects ( Include Array,Map,Set,String,TypedArray,arguments Object etc.
// Array
const arr = ['blueheart', 'zhleon', 'xiaosheng'];
for (let v of arr) {
console.log(v); // blueheart zhleon xiaosheng
}
// Set
var names = new Set(['blueheart', 'zhleon']);
for (var e of names) {
console.log(e); // blueheart zhleon
}
// Class array object arguments
function printArgs() {
for (let x of arguments) {
console.log(x);
}
}
printArgs('blueheart', 'zhleon'); // blueheart zhleon
边栏推荐
- 学好C语言从关键字开始
- Servlet
- Shandong University machine learning final 2021
- ERROR 1215 (HY000): Cannot add foreign key constraint
- Instanceof and type conversion
- Detailed steps for installing mysql-5.6.16 64 bit green version
- EasyGBS接入的设备视频直播突然全部无法播放是为什么?数据库读写不够
- CCS安装编译器的方法
- Chapter 1 of machine learning [series] linear regression model
- Which company is better in JIRA organizational structure management?
猜你喜欢

PHP laravel8 send email

Teach you to write word formula

Learn C language well from keywords

通过两种方式手写一个消息队列

This is probably the most comprehensive project about Twitter information crawler search on the Chinese Internet

SQLI_ LIBS range construction and 1-10get injection practice

Simple understanding of pseudo elements before and after

Sqli-libs post injection question 11-17 actual combat

jenkins-凭证管理

Moteur de modèle de moteur thymeleaf
随机推荐
This point of arrow function
LeetCodeT526
JIRA software annual summary: release of 12 important functions
What is sentinel produced by Ali?
不同VLAN间的通信
Use of constructors
URL in flask_ for
Training and testing of super score model in mmediting
Sqli-libs range 23-24 filtration and secondary injection practice
Print sparse arrays and restore
个人常用软件及浏览器插件分享
Goodbye 2021 Hello 2022
FPGA interview notes (II) -- synchronous asynchronous D flip-flop, static and dynamic timing analysis, frequency division design, retiming
使用Meshlab对CAD模型采样点云,并在PCL中显示
解决ffmpeg获取AAC音频文件duration不准
Excellent practice | how to avoid a bloody case caused by a line of wrong code?
Do you know the functions of getbit and setbit in redis?
End of 2021 graphics of Shandong University
C language war "minesweeping"
Servlet