# iteration

[JS]() ⟩ iteration

{% hint style="success" %}
<mark style="color:yellow;">**three types**</mark> that are related to <mark style="color:purple;">**iteration**</mark>：

* [iterable](iteration/iterable "mention") - object that <mark style="color:yellow;">**can**</mark> [**make iterators**](iteration/iterable/make-iterator-method) to loop over itself.
* [iterator](iteration/iterator "mention") - object that <mark style="color:yellow;">**can**</mark> [**produce the next iteration result**](iteration/iterator/next).
* [iteration-result](iteration/iteration-result "mention") - object that holds the [**result**](iteration/iteration-result) <mark style="color:yellow;">**of**</mark>**&#x20;**<mark style="color:orange;">**each step**</mark> of the iteration.
{% endhint %}

{% tabs %}
{% tab title="🔴 主題" %}
* <mark style="color:yellow;">**custom extension**</mark>
  * [iterable+ext](iteration/iterable+ext "mention")
* [loop](grammar/statement/loop "mention") <mark style="color:yellow;">**statements for iterations**</mark>
  * [for](grammar/statement/loop/for/for "mention") / [in](grammar/statement/loop/for/in "mention") / [of](grammar/statement/loop/for/of "mention") / [await](grammar/statement/loop/for/await "mention")
  * [while](grammar/statement/loop/while "mention"), [do...while](grammar/statement/loop/do...while "mention")
* [iterable](iteration/iterable "mention")
  * [make-iterator-method](iteration/iterable/make-iterator-method "mention")
  * [only-iterate-once](iteration/iterable/only-iterate-once "mention")
  * [make-iterables](iteration/iterable/make-iterables "mention")
  * [make-iterable](iteration/iterator/make-iterable "mention")
  * [custom](iteration/iterable/custom "mention")
* [iterator](iteration/iterator "mention")
  * [next](iteration/iterator/next "mention") - iterators must implement next() method.&#x20;
  * [make-iterator-method](iteration/iterable/make-iterator-method "mention") of an [iterable](iteration/iterable "mention") - iterables can make iterators.
  * [iterators-only-iterate-once](iteration/iterator/iterators-only-iterate-once "mention") - iterators only iterate once.
  * [iterable](iteration/iterator/iterable "mention") - an **iterator** that is itself [iterable](iteration/iterable "mention").
  * [make-iterable](iteration/iterator/make-iterable "mention")
  * [iteratorprototype](iteration/iterator/iteratorprototype "mention") - [prototype](val/obj/proto) of all <mark style="color:orange;">**built-in**</mark>**&#x20;**<mark style="color:yellow;">**iterators**</mark>.
  * [iterator](iteration/iterator/iterator "mention") - <mark style="color:yellow;">**extension**</mark> for (<mark style="color:orange;">**built-in**</mark>) <mark style="color:yellow;">**iterators**</mark>.
* [iteration-result](iteration/iteration-result "mention")
* [iteration](iteration "mention")
  * **three types** that are related to the [iteration](iteration "mention").
    * [iterable](iteration/iterable "mention") - object that <mark style="color:yellow;">**can be**</mark>**&#x20;**<mark style="color:orange;">**iterated**</mark>.
    * [iterator](iteration/iterator "mention") - object that <mark style="color:yellow;">**performs the**</mark>**&#x20;**<mark style="color:orange;">**iteration**</mark>.
    * [iteration-result](iteration/iteration-result "mention") - object that holds the <mark style="color:yellow;">**result of**</mark>**&#x20;**<mark style="color:orange;">**each step**</mark> of the iteration.
  * [generator](iteration/generator "mention") - objects returned by [func](iteration/generator/func "mention").
  * [func](iteration/generator/func "mention") - return [Broken link](broken-reference "mention").
  * [examples](iteration/generator/examples "mention")
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="info" %}
to iterate an [iterable](iteration/iterable "mention") object (<mark style="color:red;">**the hard way**</mark>)：

1. call [iterable](iteration/iterable "mention") object's [<mark style="color:yellow;">**iterator method**</mark>](iteration/iterable/make-iterator-method) to get an [iterator](iteration/iterator "mention").
2. call the [iterator](iteration/iterator "mention")'s <mark style="color:blue;">**next()**</mark> method repeatedly until the [iteration-result](iteration/iteration-result "mention") is <mark style="color:yellow;">**done**</mark>.
{% endhint %}
{% endtab %}

{% tab title="🗺️ 圖解" %}
<img src="https://2527454625-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfvEFZnSBhKT6fJmus0%2Fuploads%2FLDbsdTPR3ENAv8P81akq%2Fiteration-related-types.svg?alt=media&#x26;token=6c6da705-70d0-4fd6-97b8-5663b3d61f2d" alt="" class="gitbook-drawing">
{% endtab %}

{% tab title="💈範例" %}
:floppy\_disk: replit：[iterator](https://replit.com/@pegasusroe/iterator#index.js)

```javascript
let iterable = [1, 2, 3];

// ⭐️ iterate an iterable "the hard way"
let iterator = iterable[Symbol.iterator]();  // ⭐️ 1. make an iterator
let result = iterator.next();                // ⭐️ 2. first result

while (!result.done) {
    console.log(result.value);
    result = iterator.next();                // call next() repeatedly
}

// ⭐️ the easy way
for (const i of iterable) { console.log(i) } // 1, 2, 3
```
{% endtab %}

{% tab title="📘 手冊" %}
* [ ] [Statements and declarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements) ⟩ [iterations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements#iterations)
* [ ] [Iterators and generators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)
  * [ ] [iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
  * [ ] [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator)
* [ ] [Loops and iteration](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration)
  * [ ] [for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
* [ ] [Symbol.iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator) ( = makeIterator in Swift )
{% endtab %}

{% tab title="📗 參考" %}
* [ ] ExploringJS ⟩ [21. Iterables and Iterators](https://exploringjs.com/es6/ch_iteration.html) ⭐️
* [ ] JavaScript: The Definitive Guide ⟩ 12. Iterators and Generators
* [ ] JS.info ⟩&#x20;
  * [ ] [iterables](https://javascript.info/iterable)
  * [ ] [Generators](https://javascript.info/generators)
* [ ] 2ality ⟩ [JavaScript needs more helper functions for iteration (map, filter, etc.)](https://2ality.com/2021/08/iteration-helpers.html)
* [x] [認識 JavaScript Iterable 和 Iterator](https://jiepeng.me/2018/04/19/iterable-and-iterator-in-javascript)
{% endtab %}
{% endtabs %}
