Iterator.prototype.take()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

take()Iterator インスタンスのメソッドで、このイテレーター内で指定された数の要素を生成し、その後終了する新しいイテレーターヘルパーオブジェクトを返します。

構文

js
take(limit)

引数

limit

反復処理の先頭から取り出す要素の数です。

返値

新しいイテレーターヘルパーオブジェクトです。返されたイテレーターヘルパーは、元のイテレーターの要素を 1 つずつ生成し、 limit の数の要素が生成された場合、または元のイテレーターがすべて処理された場合(next() メソッドが { value: undefined, done: true } を生成した場合)に、完了します。

例外

RangeError

limit整数に変換した場合に NaN または負の数になった場合。

take() の使用

次の例では、フィボナッチ数列の項目を反復処理するイテレーターを作成し、最初の 3 つの項目をログ記録します。

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

take() を for...of ループで使用

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)) {
  // 最初の 2 つの要素を捨て、次の 5 つを取る
  console.log(n);
}
// ログ出力:
// 2
// 3
// 5
// 8
// 13

for (const n of fibonacci().take(5).drop(2)) {
  // 最初の5つの要素を取り、その次の 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

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
take

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.
Has more compatibility info.

関連情報