Iterator.prototype.take()
Baseline 2025Newly available
Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
语法
js
take(limit)
参数
limit
要从迭代器的开头获取的元素数量。
返回值
一个新的迭代器辅助方法。返回的迭代器辅助方法逐个生成原始迭代器中的元素,并在生成 limit
个元素后(或原始迭代器耗尽时)完成(next()
方法产生 { value: undefined, done: true }
)。
异常
示例
使用 take()
下面的示例创建了一个生成斐波那契数列中的项的迭代器,然后打印前三个项:
js
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
在 for...of 循环中使用 take()
当你不想手动快进迭代器时,take()
是最方便的。因为迭代器也是可迭代的,所以你可以用 for...of
循环来迭代返回的辅助方法。
js
for (const n of fibonacci().take(5)) { console.log(n); } // 输出: // 1 // 1 // 2 // 3 // 5
因为 fibonacci()
是一个无限迭代器,你无法直接使用 for
循环来迭代它。
将 drop() 与 take() 结合使用
你可以将 take()
与 Iterator.prototype.drop()
结合使用来获取迭代器的切片:
js
for (const n of fibonacci().drop(2).take(5)) { // 丢弃第一个元素,然后取接下来的五个元素 console.log(n); } // 输出: // 2 // 3 // 5 // 8 // 13 for (const n of fibonacci().take(5).drop(2)) { // 取前五个元素,然后丢弃其中的前两个元素 console.log(n); } // 输出: // 2 // 3 // 5
获取元素数量的下限和上限
当 limit
为负数或 NaN
时,会抛出 RangeError
异常:
js
fibonacci().take(-1); // RangeError: -1 must be positive fibonacci().take(undefined); // RangeError: undefined must be positive
当 limit
大于该迭代器可生成的元素总数时(比如 Infinity
),返回的迭代器辅助方法的行为与原始迭代器基本相同:
js
for (const n of new Set([1, 2, 3]).values().take(Infinity)) { console.log(n); } // 输出: // 1 // 2 // 3
规范
Specification |
---|
Iterator Helpers # sec-iteratorprototype.take |