当前位置:网站首页>Es6: iterator
Es6: iterator
2022-06-26 13:18:00 【HaanLen】
Iterator
Iterator yes ES6 A new traversal mechanism is introduced , Iterators have two core concepts :
- Iterators are a unified interface , Its function is to make all kinds of data structures easily accessible , It's through a key for
Symbol.iteratorTo achieve this . - Iterators are pointers used to traverse data structure elements ( Such as cursor in database ).
iterative process
- adopt
Symbol.iteratorCreate an iterator , Point to the start of the current data structure - Subsequently passed
nextMethod iterates down to the next location ,nextMethod returns the object in the current location , The object containsvalueanddoneTwo attributes , value Is the value of the current property , done Used to determine whether traversal ends - When done by
trueThe traversal ends
Examples show
const items=[1,3,4,6,8,9];
const ss=items[Symbol.iterator]();
let a1=ss.next();
console.log(a1);

Iterative data structures
Array、String、Map、Set
for ...of loop
【1】Array
const items=[1,3,4,6,8,9];
for(let item of items){
console.log(item);
}
【2】String
const items="stringforsomething"
for(let item of items){
console.log(item);
}
【3】Map
Traverse key and value
let maps=new Map();
maps.set(0,"hello");
maps.set(1,"hello1");
maps.set(2,"hello2");
maps.set(3,"hello3");
for(let [key,value] of maps){
console.log(key+"="+value);
}
let maps=new Map();
maps.set(0,"hello");
maps.set(1,"hello1");
maps.set(2,"hello2");
maps.set(3,"hello3");
for(let [key,value] of maps.entries()){
console.log(key+"="+value);
}
Just traverse key
let maps=new Map();
maps.set(0,"hello");
maps.set(1,"hello1");
maps.set(2,"hello2");
maps.set(3,"hello3");
for(let key of maps.keys()){
console.log(key);
}
Just traverse value
let maps=new Map();
maps.set(0,"hello");
maps.set(1,"hello1");
maps.set(2,"hello2");
maps.set(3,"hello3");
for(let value of maps.values()){
console.log(value);
}
【4】Set
Traverse the whole set
let sets=new Set();
sets.add(12);
sets.add('hello');
sets.add('hello2');
sets.add('hello3');
sets.add('hello4');
for(let item of sets){
console.log(item);
}
Just traverse key
let sets=new Set();
sets.add(12);
sets.add('hello');
sets.add('hello2');
sets.add('hello3');
sets.add('hello4');
for(let key of sets.keys()){
console.log(key);
}
Just traverse value
let sets=new Set();
sets.add(12);
sets.add('hello');
sets.add('hello2');
sets.add('hello3');
sets.add('hello4');
for(let value of sets.values()){
console.log(value);
}
Traverse key and value
let sets=new Set();
sets.add(12);
sets.add('hello');
sets.add('hello2');
sets.add('hello3');
sets.add('hello4');
for(let [key,value] of sets.entries()){
console.log(key,value);
}

Iteration of normal objects
const arrayLink = {
length: 3, 0: "zero", 1: "one",2:"hello"}
for(let item of Array.from(arrayLink)){
console.log(item);
}

边栏推荐
- 2、并行接口、协议和相关芯片介绍(8080、8060)
- Composite mode
- Electron official docs series: Processes in Electron
- Fire warning is completed within 10 seconds, and Baidu AI Cloud helps Kunming Guandu build a new benchmark of smart city
- UVA10341 solve it 二分
- J - Wooden Sticks poj 1065
- What features are added to Photoshop 2022 23.4.1? Do you know anything
- Analysis of state transition diagram of Beifu NC axis
- HDU 3555 Bomb
- H5视频自动播放和循环播放
猜你喜欢

Processsing mouse interactive learning

Appearance mode (facade)

Dark horse notes - Common APIs

Processing polyhedron change

Analysis of state transition diagram of Beifu NC axis

Machine learning notes - seasonality of time series

Electron official docs series: Get Started

Electron official docs series: Processes in Electron

倍福TwinCAT3实现CSV、TXT文件读写操作

外观模式(Facade)
随机推荐
mysql讲解(一)
Decorator
Beifu realizes the control of time slice size and quantity through CTU and ton
偶言佳句,孤芳自赏
What are the common categories of software testing?
Common faults of MySQL database - forgetting database password
Electron official docs series: Distribution
What should the software test report include? Interview must ask
Prototype
tauri vs electron
Processing polyhedron change
Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program
H - Sumsets POJ 2229
To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
Word document export (using fixed template)
Beifu twincat3 can read and write CSV and txt files
J - Wooden Sticks poj 1065
QT . Establishment and use of pri
G - Cow Bowling
C - Common Subsequence