Home > Web Front-end > JS Tutorial > Simple usage example of for...of in ES6

Simple usage example of for...of in ES6

不言
Release: 2018-11-14 16:32:28
Original
1954 people have browsed it

This article brings you a simple usage example of for...of in ES6. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Overview

for...of is a way to iterate iterable objects. Iterable objects include Array, Map, Set, String, TypedArray, arguments objects, etc.

Syntax

for(variable of iterable){
    // statement
}
Copy after login

Iteration Array

for(let a of [1,2,3]){
    console.log(a)
} 
// 1
// 2
// 3
Copy after login

Iteration String

for(let s of 'hello'){
    console.log(s)
}
// h
// e
// l
// l
// o
Copy after login

Iteration Set

for(let s of new Set([1,2,3])){
    console.log(s)
}
// 1
// 2
// 3
Copy after login

0x004 IterationMap

for(let s of new Map([[1,1],[2,2]])){
    console.log(s)
}
// (2) [1, 1]
// (2) [2, 2]
Copy after login

Iterate arguments

(function() {
  for (let argument of arguments) {
    console.log(argument);
  }
})(1, 2, 3);
Copy after login

Iterate Dom collection

for(let p of document.getElementsByTagName('p')){
    console.log(p)
}
// <p>...<p>
// <p>...<p>
// <p>...<p>
// <p>...<p>
...
Copy after login

Summary

for...of can only iterate iterable objects


The above is the detailed content of Simple usage example of for...of in ES6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template