Skip to content

Instantly share code, notes, and snippets.

@hg-pyun
Created October 4, 2018 14:25
Show Gist options
  • Select an option

  • Save hg-pyun/c8fc0b1e058afd51343d6ed077818da0 to your computer and use it in GitHub Desktop.

Select an option

Save hg-pyun/c8fc0b1e058afd51343d6ed077818da0 to your computer and use it in GitHub Desktop.

Revisions

  1. hg-pyun created this gist Oct 4, 2018.
    45 changes: 45 additions & 0 deletions iterator.01.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    /* array */
    let iterable = [10, 20, 30];

    for (let value of iterable) {
    console.log(value); // 10 20 30
    }

    /* string */
    let iterable = "boo";

    for (let value of iterable) {
    console.log(value); // "b" "o" "o"
    }

    /* Typed Array */
    let iterable = new Uint8Array([0x00, 0xff]);

    for (let value of iterable) {
    console.log(value); // 0 255
    }

    /* Map */
    let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

    for (let entry of iterable) {
    console.log(entry); // [a, 1] [b, 2] [c, 3]
    }

    for (let [key, value] of iterable) {
    console.log(value); // 1 2 3
    }

    /* Set */
    let iterable = new Set([1, 1, 2, 2, 3, 3]);

    for (let value of iterable) {
    console.log(value); // 1 2 3
    }

    /* DOM Collection */
    let articleParagraphs = document.querySelectorAll("article > p");

    for (let paragraph of articleParagraphs) {
    paragraph.classList.add("read");
    }