The loop method in JavaScript: foreach,for-in,for-of

Source: Internet
Author: User
Tags iterable new set prev

JavaScript has been in existence for more than 20 of years, and the way we've been using it to loop an array is this:

for (var index = 0; index < myarray.length; index++) {Console.log (Myarray[index]);}

Since JavaScript5, we have been able to use the built-in foreach method:

Myarray.foreach (function (value) {Console.log (value);});

It's a lot easier to do, but there are drawbacks: you can't interrupt loops (using the break statement or using the return statement.)

There is also a looping method in JavaScript: For–in.

The for-in loop is actually designed for the cyclic "enumerable" object:

var obj = {a:1, b:2, c:3};    For (var prop in obj) {  console.log ("obj." + prop + "=" + Obj[prop]);} Output://"OBJ.A = 1"//"OBJ.B = 2"//"OBJ.C = 3"

You can also use it to loop an array:

For (var index in MyArray) {//Do not recommend such Console.log (Myarray[index]);}

It is not recommended to use for-in to loop an array because, unlike an object, the index of an array is not the same as a normal object property, which is an important numerical sequence indicator.

In summary, For–in is the method used to loop an object with a string key.

for-of Cycle

JAVASCRIPT6 introduces a new loop method, which is the for-of cycle, which is simpler than the traditional for loop, and compensates for the short boards of the foreach and for-in loops.

Let's take a look at its for-of syntax:

for (var value of MyArray) {  console.log (value);}

For-of's syntax looks similar to for-in, but it has a much richer function, and it can loop a lot of things.

examples of for-of cycle use: Loop an array:
Let iterable = [ten, 30];for (let value of Iterable) {Console.log (value);} 10//20//30

We can use const instead of let, so it becomes a non-modifiable static variable in the loop.

Let iterable = [ten, 30];for (const value of iterable) {Console.log (value);} 10//20//30
loop A string:
Let iterable = "Boo"; for (let value of Iterable) {Console.log (value);} "B"//"O"//"O"
loop A typed array (TypedArray):
Let iterable = new Uint8array ([0x00, 0xFF]); for (let value of Iterable) {Console.log (value);} 0//255
loop A map:
Let iterable = new Map ([["A", 1], ["B", 2], ["C", 3]]), and for (let [key, value] of iterable) {Console.log (value);} 1//2//3for (Let entry of iterable) {Console.log (entry);} [A, 1]//[B, 2]//[C, 3]
loop a Set:
Let iterable = new Set ([1, 1, 2, 2, 3, 3]); for (let value of Iterable) {Console.log (value);} 1//2//3
Loop a DOM collection

Looping a DOM collections, such as nodelist, before we discussed how to loop a nodelist, now convenient, you can directly use the for-of loop:

Note:this would work in platforms, have//implemented Nodelist.prototype[symbol.iterator]let articleparagraphs = Document.queryselectorall ("article > P"); for (let paragraph of articleparagraphs) {Paragraph.classList.add ("read" );}
loops An object that owns the enumerable property

For–of loops cannot be used directly on ordinary objects, but if we loop through the properties owned by the object, we can use the built-in Object.keys () method:

for (var key of Object.keys (Someobject)) {Console.log (key + ":" + Someobject[key]);}
loop a generator (generators)

We can loop a generator (generators):

function* Fibonacci () {//a generator function let [prev, curr] = [0, 1];    while (true) {[Prev, curr] = [Curr, prev + Curr];  Yield Curr;  }}for (Let N of Fibonacci ()) {Console.log (n);  Truncate the sequence at a (n >=) {break; }}

The loop method in JavaScript: foreach,for-in,for-of

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.