Iterator.prototype.filter()

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

Iterator 实例的 filter() 方法返回一个新的迭代器辅助方法,该迭代器辅助方法只生成迭代器中能令提供的回调函数返回 true 的元素。

语法

js
filter(callbackFn)

参数

callbackFn

为迭代器中的每个元素执行的函数。它应该返回一个真值来使迭代器辅助方法产生对应元素,否则返回一个假值。该函数被调用时将传入以下参数:

element

当前正在处理的元素。

index

当前正在处理的元素的索引。

返回值

一个新的迭代器辅助方法。每当迭代器辅助方法的 next() 方法被调用时,它都会返回一个能令回调函数返回 true 的元素。当底层迭代器完成时,迭代器辅助方法也完成(next() 方法产生 { value: undefined, done: true })。

描述

迭代器辅助方法相对于数组方法的主要优势在于它们能够处理无限迭代器。对于无限迭代器,filter() 允许你只迭代满足给定条件的元素。

示例

使用 filter()

下面的示例创建了一个生成斐波那契数列中的项的迭代器,然后读取前几个偶数项:

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

const seq = fibonacci().filter((x) => x % 2 === 0);
console.log(seq.next().value); // 2
console.log(seq.next().value); // 8
console.log(seq.next().value); // 34

在 for...of 循环中使用 filter()

当你不想手动快进迭代器时,filter() 是最方便的。因为迭代器也是可迭代的,所以你可以用 for...of 循环来迭代返回的辅助方法。

js
for (const n of fibonacci().filter((x) => x % 2 === 0)) {
  console.log(n);
  if (n > 30) {
    break;
  }
}

// 输出:
// 2
// 8
// 34

等价于:

js
for (const n of fibonacci()) {
  if (n % 2 !== 0) {
    continue;
  }
  console.log(n);
  if (n > 30) {
    break;
  }
}

规范

Specification
Iterator Helpers
# sec-iteratorprototype.filter

浏览器兼容性

BCD tables only load in the browser

参见