大网数据广告活动
Web 标准与前端开发

Iterator.prototype.windows()

· 52CE 我爱测

The **`windows()`** method of Iterator instances returns a new iterator helper object that yields a sliding window of elements. Each time the helper is iterated, it yields an array removing the first element from the previous iteration and adding the next element from the origina

The windows() method of Iterator instances returns a new iterator helper object that yields a sliding window of elements. Each time the helper is iterated, it yields an array removing the first element from the previous iteration and adding the next element from the original iterator.

To yield distinct chunks, see Iterator.prototype.chunks().

Syntax

windows(windowSize)
windows(windowSize, underSized)

Parameters

  • windowSize
    • : The number of elements in the sliding window. Must be a positive integer less than 2<sup>32</sup> (the maximum array length).
  • underSized
    • : A string specifying what to do when the number of elements in the iterator is less than windowSize but greater than 0. It can be one of the following:

      • "only-full" (default)
        • : Ignore the partial window. The returned iterator immediately completes as if the input is empty.
      • "allow-partial"
        • : Yield all elements in the partial window as an array (with length less than windowSize).

      Note that when the input is empty, the returned iterator always immediately completes without yielding an empty array.

Return value

A new iterator helper object. The first time the returned iterator helper's next() method is called, the current iterator is immediately advanced by windowSize elements. If that many elements are available, they are yielded together as an array. Otherwise, the behavior depends on the underSized argument: the iterator either immediately completes without yielding anything (with "only-full" or with a fully empty input), or an array of length less than windowSize is yielded.

Afterwards, each time, the yielded array consists of the previous array, the leftmost element removed, a new element retrieved from the input iterable appended. When the input iterable is exhausted, the iterator helper also completes.

Exceptions

  • TypeError
    • : Thrown if limit is not an integer, or if underSized is not one of the allowed values.
  • RangeError
    • : Thrown if limit is less than 1 or greater than 2<sup>32</sup>-1.

Examples

Moving average

The following example creates an iterator that yields terms in the Fibonacci sequence. Then, a new iterator is created, each time yielding the average of two adjacent elements.

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

const averaged = fibonacci()
  .windows(2)
  .map((window) => (window[0] + window[1]) / 2);

console.log(averaged.next().value); // 1 (1, 1)
console.log(averaged.next().value); // 1.5 (1, 2)
console.log(averaged.next().value); // 2.5 (2, 3)
console.log(averaged.next().value); // 4 (3, 5)

This process, known as a moving average, is very useful in signal processing.

Enumerating substrings

The following example enumerates all substrings of a given length in a string. The windows() method is used to create a sliding window of characters, which are then joined together to form substrings.

[!NOTE] This is probably much less efficient than manually iterating a number index and calling the String.prototype.substring() method.

const chars = Iterator.from("MASSACHUSETTS");

const substrings = chars.windows(3).map((window) => window.join(""));

for (const substring of substrings) {
  console.log(substring);
}
// MAS
// ASS
// SSA
// ...

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。