当前位置:网站首页>ES6:迭代器
ES6:迭代器
2022-06-26 12:36:00 【HaanLen】
Iterator
Iterator 是 ES6 引入的一种新的遍历机制,迭代器有两个核心概念:
- 迭代器是一个统一的接口,它的作用是使各种数据结构可被便捷的访问,它是通过一个键为
Symbol.iterator的方法来实现。 - 迭代器是用于遍历数据结构元素的指针(如数据库中的游标)。
迭代过程
- 通过
Symbol.iterator创建一个迭代器,指向当前数据结构的起始位置 - 随后通过
next方法进行向下迭代指向下一个位置,next方法会返回当前位置的对象,对象包含了value和done两个属性, value 是当前属性的值, done 用于判断是否遍历结束 - 当 done 为
true时则遍历结束
例子展示
const items=[1,3,4,6,8,9];
const ss=items[Symbol.iterator]();
let a1=ss.next();
console.log(a1);

可迭代的数据结构
Array、String、Map、Set
for ...of循环
【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
遍历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){
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);
}
只遍历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);
}
只遍历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
遍历整个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);
}
只遍历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);
}
只遍历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);
}
遍历key和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);
}

普通对象的迭代
const arrayLink = {
length: 3, 0: "zero", 1: "one",2:"hello"}
for(let item of Array.from(arrayLink)){
console.log(item);
}

边栏推荐
- dried food! Yiwen will show you SD card, TF card and SIM card!
- 小程序中控件里面的内容较多,让其支持滚动的良好方案
- Analysis report on China's photovoltaic inverter market prospect forecast and investment strategy recommendations in 2022
- 文件远程同步、备份神器rsync
- Microservice governance (nocas)
- 详细实操分享,下班刷了两小时的搞笑视频,一个月收益7000多
- JS get the current screen height method and listen for DOM elements to enter the viewport
- Implementing mixins scheme in applet
- Redis learning - 03 transaction
- 初识-软件测试
猜你喜欢

File remote synchronization and backup artifact Rsync

Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology

快手实时数仓保障体系研发实践

dried food! Yiwen will show you SD card, TF card and SIM card!

Build Pikachu shooting range and introduction

Spark-day02-core programming-rdd

Microservice governance (nocas)

Scala-day06- pattern matching - Generic

Xiaolong 888 was released, Xiaomi 11 was launched, and 14 manufacturers carried it in the first batch!

初识-软件测试
随机推荐
2022 edition of investment analysis and "fourteenth five year plan" development prospect forecast report of China's switchgear industry
I want to know whether flush is a stock market? Is online account opening safe?
一个快速切换一个底层实现的思路分享
PHP generate order number
Redis learning - 05 node JS client operation redis and pipeline pipeline
nvm安装教程
手把手带你学会Odoo OWL组件开发(7):OWL项目实战使用
Basic principle of MOS tube and important knowledge points of single chip microcomputer
dried food! Yiwen will show you SD card, TF card and SIM card!
2022 edition of China's energy and chemical industry market in-depth investigation and investment feasibility analysis report
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
Laravel subdomain accesses different routing files and different modules
Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
Encapsulate request request of uni app
Less than 40 lines of code to create a blocprovider
由错误<note: candidate expects 1 argument, 0 provided>引发的思考
Scala problem solving the problem of slow SBT Download
New routing file in laravel framework
Spark-day02-core programming-rdd
Current situation investigation and investment prospect forecast analysis report of China's electrolytic copper market from 2022 to 2028