JS: Iterable Object
New in JS2015.
What is Iteratable Object
An iterable object holds special values to be iterated over.
An iterable object can have potentially infinite number of items, computed at the time when needed. As opposed to a array, where every item already exists and occupy memory.
Example of Iterable's Values
- String is iterable. Characters are the iterated values.
- Array is iterable. Array items are the iterated values.
- Map Object is iterable. Keys are the iterated values.
Iterated values can be anything the iteratable object is designed for, such a list of prime numbers.
How to Iterate Over Iterable's Values
- JS: for-of Loop
- JS: Spread Operator (triple dots) → convert it to array or function arguments.
- JS: Array.from → convert it to array.
Standard Iterable Objects
const is_iterable = ((x) => (Reflect.has(x, Symbol.iterator))); console.log( is_iterable(String.prototype), is_iterable(Array.prototype), is_iterable(Set.prototype), is_iterable(Map.prototype), );
The Iterable Interface
Technically, any object that conforms to the Iterable Interface is called Iterable.
Define Your Own Iterable Object
You can create your own iterable object using Generator Function .