JS: Iterator.prototype.take
(new in JS: ECMAScript 2025)
iterator.take(n)
-
- Take the first n yield of iterator.
- Return a new Generator.
Normally you can use this method on any Iterable, because all standard iterable objects are also Iterator.
// define a generator function function* gf() { for (let x of [1, 2, 3, 4, 5]) yield x; } console.log( JSON.stringify(Array.from( gf().take(3), )) === `[1,2,3]`, ); // true // is iterable console.log(Reflect.has(gf().take(3), Symbol.iterator)); // true // is iterator console.log(Reflect.has(gf().take(3), "next")); // true
JavaScript. iterator helpers
- JS: Iterator.prototype.map
- JS: Iterator.prototype.forEach
- JS: Iterator.prototype.filter
- JS: Iterator.prototype.reduce
- JS: Iterator.prototype.take
- JS: Iterator.prototype.drop
- JS: Iterator.prototype.every
- JS: Iterator.prototype.some
- JS: Iterator.prototype.find
- JS: Iterator.prototype.flatMap
- JS: Iterator.prototype.toArray