PrefaceEvery developer who comes into contact with JS will inevitably deal with the for loop. After all, it is one of the essential tools for traversal. I believe everyone is getting tired of the for loop statement in JavaScript. There are many articles talking about how to reduce the for loop statements in the code, but you have to admit that they are really useful. Today, I will summarize three for loop statements in front-end JavaScript. forThis is probably the most widely used loop statement. It is simple and practical, and its performance is still online most of the time. The only disadvantage is that it is too ordinary and has no characteristics, which makes many people unwilling to use it now. const array = [4, 7, 9, 2, 6]; for (let index = 0; index < array.length; index++) { const element = array[index]; console.log(element); } // 4, 7, 9, 2, 6 for...inThe for...in statement can iterate over an object's enumerable properties, except Symbol, in any order. const temp = {name: "temp"}; function Apple() { this.color = 'red'; } Apple.prototype = temp; const obj = new Apple(); for (const prop in obj) { console.log(`obj.${ prop } = ${ obj[prop] }`); } // obj.color = red // obj.name = temp If you only care about the properties of the object itself, not its prototype, then use getOwnPropertyNames() or perform hasOwnProperty() to determine whether a property is a property of the object itself. const temp = {name: "temp"}; function Apple() { this.color = 'red'; } Apple.prototype = temp; const obj = new Apple(); for (const prop in obj) { if (obj.hasOwnProperty(prop)) { console.log(`obj.${ prop } = ${ obj[prop] }`); } } // obj.color = red Of course, it can also be used to traverse an array. const arr = [1, 2, 3, 4, 5]; for (const key in arr) { console.log(key) } // 0,1,2,3,4 You can use for...in to traverse an array, but there are the following problems:
Therefore, it is generally not recommended to use for...in to traverse an array. for...ofThe for...of statement creates an iteration loop over an iterable object (including Array, Map, Set, String, TypedArray, arguments object, etc.), calls the custom iteration hook, and executes statements for each different property value. const array = ['a', 'b', 'c']; for (const element of array) { console.log(element); } // a // b // c The difference between for...of and for...in:
Object.prototype.objCustom = function () { }; Array.prototype.arrCustom = function () { }; let iterable = [3, 5, 7]; iterable.foo = 'hello'; for (const key in iterable) { console.log(key); // logs 0, 1, 2, "foo", "arrCustom", "objCustom" } // 0, 1, 2, "foo", "arrCustom", "objCustom" for (const key of iterable) { console.log(key); } // 3, 5, 7 Use for...of to traverse the Map structure: let nodes = new Map(); nodes.set("node1", "t1") .set("node2", "t2") .set("node3", "t3"); for (const [node, content] of nodes) { console.log(node, content); } // node1 t1 // node2 t2 // node3 t3 It can be seen that it is quite convenient to use for...of to traverse the Map structure. It is recommended to use it! Summarize
This concludes this article on the use of three for loop statements in JavaScript. For more information about for loop statements in JS, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to remove MySQL from Ubuntu and reinstall it
>>: Solutions to black screen when installing Ubuntu (3 types)
Q: I don’t know what is the difference between xml...
1. Let’s take a look at the effect first Data ret...
Before we begin, we create two tables to demonstr...
This article shares with you the tutorial of inst...
To perform incremental backup of the MySQL databa...
I upgraded my Raspberry Pi server to Ubuntu 20 tw...
The solution to the problem that mysql cannot be ...
The computer system is: win7 This article is main...
In HTML, the Chinese phrase “學好好學” can be express...
Table of contents Preface 1. Life cycle in Vue2 I...
Table of contents 1. Background 2. What is a virt...
This article example shares the specific code for...
Preface When we use query statements, we often ne...
Table of contents 1. Shallow copy 1. Object.assig...
<br />Just like an article, our web pages sh...