🔰iterable iterator
JS⟩ iteration ⟩ iterator ⟩ iterable iterator
an iterator that is itself iterable. (an iterator can easily be iterable by simply returning itself in its "make-iterator" method)
iterable iterators:
str.matchAll() - iterable iterator of all matches.
s returned by generator function.
s are iterable iterators.
examples:
words in sentence - str.matchAll() returns an iterable iterator.
💾 replit:iterable iterator
let array = [1, 2, 3]; // Array is iterable
// ⭐️ make an iterator
let iterator = array[Symbol.iterator]();
// first iteration result
let head = iterator.next(); // { value: 1, done: false }
// ----------------------------
// ⭐️ iterable iterator
// ----------------------------
// the iterator of a built-in iterable object is itself "iterable", i.e. it can:
// ⭐️ 1. spread itself into array elements or function arguments.
// ⭐️ 2. make an iterator for itself. (in fact, the iterator === itself)
// ⭐️ 1. spread itself into array elements
let tail = [...iterator]; // [ 2, 3 ] ⭐️
// ⭐️ 2. make an iterator for itself
let it = iterator[Symbol.iterator]();
// ⭐️ 2. the iterator of the `iterator` is the `iterator` itself.
it === iterator; // true❗Last updated
Was this helpful?