点此查看 所有教程、项目、源码导航
文章目录
- [1. 背景介绍](#1. 背景介绍)
- [2. for...of 基础用法](#2. for...of 基础用法)
- [3. 迭代器是什么](#3. 迭代器是什么)
- [4. 可迭代对象](#4. 可迭代对象)
- [5. 和 for...in 的区别](#5. 和 for...in 的区别)
- [6. 遍历其他可迭代对象](#6. 遍历其他可迭代对象)
-
- [6.1 字符串](#6.1 字符串)
- [6.2 Set](#6.2 Set)
- [6.3 Map](#6.3 Map)
- [7. for...of 的优势与局限](#7. for...of 的优势与局限)
- [8. 小结](#8. 小结)
1. 背景介绍
平时遍历数组、对象的时候,我们已经见过 for、forEach。
ES6 新增了 for...of,它是专门用来遍历可迭代对象的语法,在 Vue 里面经常用来循环处理数组、Map、Set,也是阅读源码很常见的写法。而支撑 for...of 这套能力的底层机制,就是迭代器 Iterator。理解迭代器,才能明白为什么有些类型可以用 for...of,普通对象却不行。
2. for...of 基础用法
for...of 可以直接拿到每一项的值,写法简洁。
js
const arr = [10, 20, 30]
for (const item of arr) {
console.log(item)
}
// 10
// 20
// 30
对比传统 for 循环,不需要自己维护下标,可读性更好。它会自动调用底层迭代器,不断取值,直到遍历结束。
3. 迭代器是什么
迭代器本质是一个对象,必须拥有 next 方法。每次调用 next(),返回一个包含 value 和 done 的对象:
value:当前取出的数据done:布尔值,为true代表遍历完成
下面是一个手写迭代器示例:
js
function createIterator() {
let index = 0
const arr = ["a", "b", "c"]
return {
next() {
if (index < arr.length) {
return { value: arr[index++], done: false }
} else {
return { value: undefined, done: true }
}
}
}
}
const it = createIterator()
console.log(it.next()) // { value: "a", done: false }
console.log(it.next()) // { value: "b", done: false }
console.log(it.next()) // { value: "c", done: false }
console.log(it.next()) // { value: undefined, done: true }
4. 可迭代对象
如果一个对象具备 Symbol.iterator 方法,调用该方法能够返回上面这种迭代器,它就叫做可迭代对象。
数组、字符串、Set、Map 都是原生可迭代对象,我们可以手动取出它的迭代器:
js
const arr = [10, 20, 30]
const iterator = arr[Symbol.iterator]()
console.log(iterator.next()) // { value: 10, done: false }
console.log(iterator.next()) // { value: 20, done: false }
console.log(iterator.next()) // { value: 30, done: false }
console.log(iterator.next()) // { value: undefined, done: true }
我们也可以自己给对象添加 Symbol.iterator,让自定义对象支持 for...of:
js
const obj = {
list: [1, 2, 3],
[Symbol.iterator]() {
let index = 0
return {
next: () => {
if (index < this.list.length) {
return { value: this.list[index++], done: false }
}
return { value: undefined, done: true }
}
}
}
}
for (const v of obj) {
console.log(v)
}
5. 和 for...in 的区别
很多新手容易把这两个搞混。for...in 获取的是键名,一般不推荐用来遍历数组。
js
const arr = [10, 20, 30]
for (const key in arr) {
console.log(key)
}
// 0
// 1
// 2
简单区分:
for...of:拿到值,依赖迭代器协议,适合遍历数组、Set、Map、字符串等可迭代对象for...in:拿到键名,更适合遍历普通对象,还会遍历原型链上的属性
如果想要遍历普通对象,不能直接用 for...of,可以搭配 Object.entries。
js
const user = { name: "小明", age: 18 }
for (const [key, val] of Object.entries(user)) {
console.log(key, val)
}
// name 小明
// age 18
6. 遍历其他可迭代对象
6.1 字符串
字符串属于可迭代对象,可以逐个取出字符。
js
const str = "hello"
for (const char of str) {
console.log(char)
}
// h
// e
// l
// l
// o
6.2 Set
js
const s = new Set([1, 2, 2, 3])
for (const item of s) {
console.log(item)
}
// 1
// 2
// 3
6.3 Map
Map 遍历得到的是 [键, 值] 数组。
js
const map = new Map()
map.set("a", 1)
map.set("b", 2)
for (const [k, v] of map) {
console.log(k, v)
}
// a 1
// b 2
7. for...of 的优势与局限
优势:
- 支持
break、continue、return,而forEach无法中途停止循环 - 统一一套语法遍历多种可迭代数据结构
局限:
- 不能直接遍历普通字面量对象,普通对象默认不是可迭代对象
- 如果需要下标,要配合数组的
entries()方法
js
const arr = ["苹果", "香蕉"]
for (const [index, value] of arr.entries()) {
console.log(index, value)
}
// 0 苹果
// 1 香蕉
8. 小结
- 迭代器是带有
next方法的对象,for...of本质就是自动调用迭代器的语法糖。 - 拥有
Symbol.iterator的对象才是可迭代对象,数组、字符串、Set、Map 原生满足这个条件。 - 不要和
for...in混用,后者用来获取键,遍历数组容易出现意料之外的问题。 - 如果想要用
for...of遍历普通对象,借助Object.entries是最常用方案。 - 在 Vue 中处理列表数据时,
for...of会经常出现在方法内部,搭配break跳出循环会很方便;迭代器也是后面学习生成器的基础。