当前位置:网站首页>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);
}

边栏推荐
猜你喜欢

Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏

P2393 yyy loves Maths II

Beifu PLC obtains system time, local time, current time zone and system time zone conversion through program

Chapter 10 setting up structured logging (2)

Don't mess with full_ Case and parallel_ CASE

Bridge mode

Do you know the limitations of automated testing?

Fire warning is completed within 10 seconds, and Baidu AI Cloud helps Kunming Guandu build a new benchmark of smart city

C语言:练习题二
![8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)](/img/e7/2fd8ec8d5e5473c7f84f3e3bcedca8.png)
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
随机推荐
Beifu PLC realizes zero point power-off hold of absolute value encoder -- use of bias
MySQL数据库讲解(四)
Chapter 01_ Installation and use of MySQL under Linux
Typescript
倍福PLC基于NT_Shutdown实现控制器自动关机重启
HDU 3555 Bomb
适配器模式(Adapter)
Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
I have a good word to say, and I admire myself
Hdu1724[Simpson formula for integral]ellipse
Mode pont
Uva5009 error curves three points
B - Bridging signals
tauri vs electron
Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
Uva10341 solve it
倍福PLC实现绝对值编码器原点断电保持---bias的使用
H - Sumsets POJ 2229
I - Dollar Dayz
[how to connect the network] Chapter 2 (Part 1): establish a connection, transmit data, and disconnect