Iterator.prototype.drop()

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Iterator 实例的 drop() 方法返回一个新的 迭代器助手,该 迭代器助手 会在此迭代器开头跳过给定数量的元素。

¥The drop() method of Iterator instances returns a new iterator helper that skips the given number of elements at the start of this iterator.

语法

¥Syntax

js
drop(limit)

参数

¥Parameters

limit

从迭代开始时要删除的元素数量。

返回值

¥Return value

新的 迭代器助手。第一次调用返回的迭代器助手的 next() 方法时,当前迭代器立即前进 limit 个元素,然后生成下一个元素(第 limit+1 个元素)。然后,迭代器助手会一一生成剩余元素。如果当前迭代器的元素少于 limit 个,则新的迭代器助手将在第一次调用 next() 时立即完成。

¥A new iterator helper. The first time the returned iterator helper's next() method is called, the current iterator is immediately advanced by limit elements, and then the next element (the limit+1-th element) is yielded. The iterator helper then yields the remaining elements one-by-one. If the current iterator has fewer than limit elements, the new iterator helper will be immediately completed the first time next() is called.

例外情况

¥Exceptions

RangeError

如果 limit 变为 NaN 则抛出,或者当 转换为整数 变为负值时抛出。

示例

¥Examples

使用 drop()

¥Using drop()

以下示例创建一个迭代器,该迭代器生成斐波那契数列中的项,从第三项开始,删除前两项:

¥The following example creates an iterator that yields terms in the Fibonacci sequence, starting from the 3rd term by dropping the first two terms:

js
function* fibonacci() {
  let current = 1;
  let next = 1;
  while (true) {
    yield current;
    [current, next] = [next, current + next];
  }
}

const seq = fibonacci().drop(2);
console.log(seq.next().value); // 2
console.log(seq.next().value); // 3

这相当于:

¥This is equivalent to:

js
const seq = fibonacci();
seq.next();
seq.next();

将 drop() 与 for...of 循环结合使用

¥Using drop() with a for...of loop

当你不手动滚动迭代器时,drop() 最方便。因为迭代器也是可迭代的,所以你可以使用 for...of 循环迭代返回的助手:

¥drop() 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:

js
for (const n of fibonacci().drop(2)) {
  console.log(n);
  if (n > 30) {
    break;
  }
}

// Logs:
// 2
// 3
// 5
// 8
// 13
// 21
// 34

将 drop() 与 take() 结合起来

¥Combining drop() with take()

你可以将 drop()Iterator.prototype.take() 组合起来以获得迭代器的一部分:

¥You can combine drop() with Iterator.prototype.take() to get a slice of an iterator:

js
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 drop count

limit 为负或 NaN 时,抛出 RangeError

¥When the limit is negative or NaN, a RangeError is thrown:

js
fibonacci().drop(-1); // RangeError: -1 must be positive
fibonacci().drop(undefined); // RangeError: undefined must be positive

limit 大于迭代器可以产生的元素总数(例如 Infinity)时,返回的迭代器助手将立即删除所有元素,然后在第一次调用 next() 时完成。如果当前迭代器是无限的,则返回的迭代器助手将永远不会完成。

¥When the limit is larger than the total number of elements the iterator can produce (such as Infinity), the returned iterator helper will instantly drop all elements and then be completed the first time next() is called. If the current iterator is infinite, the returned iterator helper will never complete.

js
fibonacci().drop(Infinity).next(); // Never ends
new Set([1, 2, 3]).values().drop(Infinity).next(); // { value: undefined, done: true }
new Set([1, 2, 3]).values().drop(4).next(); // { value: undefined, done: true }

规范

Specification
Iterator Helpers
# sec-iteratorprototype.drop

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看