Iterator.prototype.take()
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Iterator
实例的 take()
方法返回一个新的 迭代器助手,该 迭代器助手 在此迭代器中产生给定数量的元素,然后终止。
¥The take()
method of Iterator
instances returns a new iterator helper that yields the given number of elements in this iterator and then terminates.
语法
参数
返回值
¥Return value
新的 迭代器助手。返回的迭代器辅助程序逐一生成原始迭代器中的元素,然后在生成 limit
个元素后或当原始迭代器耗尽时(以先到者为准)完成(next()
方法生成 { value: undefined, done: true }
)。
¥A new iterator helper. The returned iterator helper yields the elements in the original iterator one-by-one, and then completes (the next()
method produces { value: undefined, done: true }
) once limit
elements have been yielded, or when the original iterator is exhausted, whichever comes first.
例外情况
示例
使用 take()
¥Using take()
以下示例创建一个迭代器,该迭代器生成斐波那契数列中的项,然后记录前三个项:
¥The following example creates an iterator that yields terms in the Fibonacci sequence, and then logs the first three terms:
function* fibonacci() {
let current = 1;
let next = 1;
while (true) {
yield current;
[current, next] = [next, current + next];
}
}
const seq = fibonacci().take(3);
console.log(seq.next().value); // 1
console.log(seq.next().value); // 1
console.log(seq.next().value); // 2
console.log(seq.next().value); // undefined
将 take() 与 for...of 循环一起使用
¥Using take() with a for...of loop
当你不手动滚动迭代器时,take()
最方便。因为迭代器也是可迭代的,所以你可以使用 for...of
循环迭代返回的助手:
¥take()
is most convenient when you are not hand-rolling the iterator. Because iterators are also iterable, you can iterate the returned helper with a for...of
loop:
for (const n of fibonacci().take(5)) {
console.log(n);
}
// Logs:
// 1
// 1
// 2
// 3
// 5
因为 fibonacci()
是无限迭代器,所以不能使用 for
循环直接迭代它。
¥Because fibonacci()
is an infinite iterator, you can't use a for
loop to iterate it directly.
将 drop() 与 take() 结合起来
¥Combining drop() with take()
你可以将 take()
与 Iterator.prototype.drop()
组合起来以获得迭代器的一部分:
¥You can combine take()
with Iterator.prototype.drop()
to get a slice of an iterator:
for (const n of fibonacci().drop(2).take(5)) {
// Drops the first two elements, then takes the next five
console.log(n);
}
// Logs:
// 2
// 3
// 5
// 8
// 13
for (const n of fibonacci().take(5).drop(2)) {
// Takes the first five elements, then drops the first two
console.log(n);
}
// Logs:
// 2
// 3
// 5
镜头计数的下限和上限
¥Lower and upper bounds of take count
当 limit
为负或 NaN
时,抛出 RangeError
:
¥When the limit
is negative or NaN
, a RangeError
is thrown:
fibonacci().take(-1); // RangeError: -1 must be positive
fibonacci().take(undefined); // RangeError: undefined must be positive
当 limit
大于迭代器可以产生的元素总数(例如 Infinity
)时,返回的迭代器助手的行为与原始迭代器基本相同:
¥When the limit
is larger than the total number of elements the iterator can produce (such as Infinity
), the returned iterator helper has essentially the same behavior as the original iterator:
for (const n of new Set([1, 2, 3]).values().take(Infinity)) {
console.log(n);
}
// Logs:
// 1
// 2
// 3
规范
Specification |
---|
Iterator Helpers # sec-iteratorprototype.take |
浏览器兼容性
BCD tables only load in the browser