[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Iterator/filter [Back]  [Original]

Iterator.prototype.filter() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Iterator.prototype.filter()

Baseline 2025
Newly available

Since 2025 3, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Iterator filter() true .

In this article

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

filter() for...of

filter() . , for...of .

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

// Logs:
// 2
// 8
// 34

.

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

Specification
ECMAScript 2027 LanguageSpecification
# sec-iterator.prototype.filter


Web Proxy Viewer  |  New URL  |  Original Page