# 数组、字符串和Map的可迭代对象和迭代器
数组的可迭代对象和迭代器
const myArray = [1, 2, 3];
// 使用for...of语句遍历数组
for (let element of myArray) {
console.log(element);
}
// 手动迭代器遍历数组
const myIterator = myArray[Symbol.iterator]();
console.log(myIterator.next()); // { value: 1, done: false }
console.log(myIterator.next()); // { value: 2, done: false }
console.log(myIterator.next()); // { value: 3, done: false }
console.log(myIterator.next()); // { value: undefined, done: true }
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
字符串的可迭代对象和迭代器
const myString = 'Hello';
// 使用for...of语句遍历字符串
for (let char of myString) {
console.log(char);
}
// 手动迭代器遍历字符串
const myIterator = myString[Symbol.iterator]();
console.log(myIterator.next()); // { value: "H", done: false }
console.log(myIterator.next()); // { value: "e", done: false }
console.log(myIterator.next()); // { value: "l", done: false }
console.log(myIterator.next()); // { value: "l", done: false }
console.log(myIterator.next()); // { value: "o", done: false }
console.log(myIterator.next()); // { value: undefined, done: true }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Map的可迭代对象和迭代器
const myMap = new Map([
['key1', 'value1'],
['key2', 'value2'],
['key3', 'value3']
]);
// 使用for...of语句遍历Map
for (let [key, value] of myMap) {
console.log(key, value);
}
// 手动迭代器遍历Map
const myIterator = myMap[Symbol.iterator]();
console.log(myIterator.next()); // { value: [ "key1", "value1" ], done: false }
console.log(myIterator.next()); // { value: [ "key2", "value2" ], done: false }
console.log(myIterator.next()); // { value: [ "key3", "value3" ], done: false }
console.log(myIterator.next()); // { value: undefined, done: true }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 自定义可迭代对象和迭代器的实现
const myIterable = {
data: ['apple', 'banana', 'orange'],
[Symbol.iterator]: function() {
let index = 0;
const data = this.data;
return {
next: function() {
if (index < data.length) {
return { value: data[index++], done: false };
} else {
return { done: true };
}
}
}
}
};
// 使用for...of语句遍历自定义可迭代对象
for (let fruit of myIterable) {
console.log(fruit);
}
// 手动迭代器遍历自定义可迭代对象
const myIterator = myIterable[Symbol.iterator]();
console.log(myIterator.next()); // { value: "apple", done: false }
console.log(myIterator.next()); // { value: "banana", done: false }
console.log(myIterator.next()); // { value: "orange", done: false }
console.log(myIterator.next()); // { done: true }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 使用可迭代对象和迭代器进行编程的示例
示例1: 生成一个可迭代对象,该可迭代对象的迭代器可以生成一个等差数列
function makeRangeIterator(start = 0, end = Infinity, step = 1) {
let nextIndex = start;
let iterationCount = 0;
const rangeIterator = {
next: function() {
let result;
if (nextIndex < end) {
result = { value: nextIndex, done: false };
nextIndex += step;
iterationCount++;
return result;
}
return { value: iterationCount, done: true };
}
};
return rangeIterator;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
示例2: 计算数组中所有数字的平均值
function average(...nums) {
const iterator = nums[Symbol.iterator]();
let sum = 0;
let count = 0;
let result = iterator.next();
while (!result.done) {
sum += result.value;
count++;
result = iterator.next();
}
return sum / count;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
示例3: 从字符串中找到第一个元音字母
function findFirstVowel(str) {
const iterator = str[Symbol.iterator]();
let result = iterator.next();
while (!result.done) {
if (isVowel(result.value)) {
return result.value;
}
result = iterator.next();
}
return undefined;
}
function isVowel(char) {
return ['a', 'e', 'i', 'o', 'u'].includes(char.toLowerCase());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15