JS: Iterator.prototype.filter
(new in ECMAScript 2025)
iterator.filter(f)-
- Apply function f to every yield in Iterator Object iterator.
- Keep only yields that f return true.
- Return a new Generator.
f is passed args: currentElement, currentIndex.
similar to Array.prototype.filter
// define a generator function function* gf() { for (let x of Array(5).keys()) yield x; } // create a generator. a generator is both iterable and iterator const xgen = gf(); // use method filter const xresult = xgen.filter((x) => (x % 2 === 0)); // verify result is correct console.assert( JSON.stringify( Array.from(xresult), ) === "[0,2,4]", );