当前位置:网站首页>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
边栏推荐
- 关于SIoU的原理和代码实现(回顾IoU、GIoU、DIoU、CIoU)
- Invert an array with for
- Detailed installation instructions for MySQL
- Fix the problem that the right mouse button does not have a vscode shortcut
- Learn C language well from keywords
- FPGA设计——乒乓操作实现与modelsim仿真
- call和apply和bind的区别
- A collection of problems on improving working frequency and reducing power consumption in FPGA design
- Metasploitabile2 target learning
- A multi classification model suitable for discrete value classification -- softmax regression
猜你喜欢

Graphsage paper reading

Metasploitabile2 target learning
![Chapter 2 of machine learning [series] logistic regression model](/img/8f/b4c302c0309f5c91c7a40e682f9269.jpg)
Chapter 2 of machine learning [series] logistic regression model
![[must see for game development] 3-step configuration p4ignore + wonderful Q & A analysis (reprinted from user articles)](/img/4c/42933ac0fde18798ed74a23279c826.jpg)
[must see for game development] 3-step configuration p4ignore + wonderful Q & A analysis (reprinted from user articles)

Who is stronger, zip or 7-Zip, and how to choose?

山东大学项目实训之examineListActivity

Teach you to write word formula

ThymeleafEngine模板引擎

Vulhub 8.1-backdoor vulnerability recurrence

Shandong University machine learning final 2021
随机推荐
Who is stronger, zip or 7-Zip, and how to choose?
Why don't we have our own programming language?
Squid agent
Jenkins voucher management
Docker安装Mysql、Redis
call和apply和bind的区别
Observer mode (listener mode) + thread pool to realize asynchronous message sending
CCS method of installing compiler
Servlet
Matlab实现均值滤波与FPGA进行对比,并采用modelsim波形仿真
个人常用软件及浏览器插件分享
Basic use of BufferedReader and bufferedwriter
[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)
PgSQL reports an error: current transaction is aborted, commands ignored until end of transaction block
Super details to teach you how to use Jenkins to realize automatic jar package deployment
JIRA software annual summary: release of 12 important functions
Sentinel annotation support - @sentinelresource usage details
MATLAB realizes mean filtering and FPGA for comparison, and uses Modelsim waveform simulation
Continuous update of ansible learning
Excellent practice | how to avoid a bloody case caused by a line of wrong code?