Loading...
const counter = {
i: 0,
next() {
return { done: this.i > 2, value: this.i++ };
},
};
counter.map((x) => x * 2); // TypeErrorconst counter = {
i: 0,
next() {
return { done: this.i > 2, value: this.i++ };
},
};
Iterator.from(counter).map((x) => x * 2).toArray(); // [0, 2, 4]An object with only next() has no helper methods; Iterator.from() wraps it so map() and toArray() work.
Iterator.from() to copy values, while it wraps the same source.Iterator.from() wraps objects that only have a next() method, so they get helper methods.map.values() or generators are not wrapped, they already have helpers.Iterator.from() and Array.from()?Array.from() reads every value into a new array right away. Iterator.from() returns a lazy iterator over the same source, without copying.
Iterator.from()?Yes. Iterator.from('abc') yields the string one code point at a time.