mirror of
https://github.com/alrayyes/wiki.git
synced 2024-12-01 15:26:23 +00:00
804 B
804 B
id | title |
---|---|
9f77a5b0-a06d-46d0-828e-d6c5fe33111d | JavaScript Iterator |
Introduction
An iterator is a pointer for traversing the elements of a data structure.
Examples
Syntax
const iterable = ["a", "b"];
const iterator = iterable[Symbol.iterator]();
console.log(iterator.next()); // { value: 'a', done: false }
console.log(iterator.next()); // { value: 'b', done: false }
console.log(iterator.next()); // { value: undefined, done: true }
TypeScript Interfaces
Expressed as interfaces in TypeScript notation:
interface Iterable {
[Symbol.iterator]() : Iterator;
}
interface Iterator {
next() : IteratorResult;
}
interface IteratorResult {
value: any;
done: boolean;
}