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

Source: Internet
Author: User
Tags iterable

JavaScript is a literal-translation scripting language, which is a dynamic type, a weak type, a prototype-based language, and a built-in support type. Its interpreter, known as the JavaScript engine, is widely used as a scripting language for the client, and is used in HTML (an application under the standard Universal markup Language) to add dynamic functionality to an HTML web Page.

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 statement break or using statement continue).

There is also a looping method in Javascript:.

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

var obj = {a:1, b:2, c:3};  for (var 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 (varin//  does 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:

Let iterable = [ten, +];  for (let value of Iterable) {console.log (value);} // Ten //  - //  -

We can use it instead, so it becomes a non-modifiable static variable in the Loop.

Let iterable = [ten, +];  for (const value of Iterable) {console.log (value);} // Ten //  - //  -

Loop a string:

Let iterable = "boo"; for(let value of Iterable) {console.log (value);}//"b"//"o"//"o"Let iterable =NewUint8array ([0x00, 0xFF]); for(let value of Iterable) {console.log (value);}//0//255Let iterable =NewMap ([["a", 1], ["b", 2], ["c", 3]]); for(let [key, value] of Iterable) {console.log (value);}//1//2//3 for(let entry of Iterable) {console.log (entry);}//[a, 1]//[b, 2]//[c, 3]Let iterable =NewSet ([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. // 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+ ":" + someobject[key]);}

Loop A generator (generators)

We can loop a generator (generators):

function // a generator functionlet [prev, curr] = [0, 1];  while (true= [curr, prev + Curr];yield curr;}}  for (let N of Fibonacci ()) {console.log (n); // truncate the sequence at if (n >=) {break;}}

Reprint to Http://www.jb51.net/article/86076.htm

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

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.