Skip to content

Instantly share code, notes, and snippets.

@hg-pyun
Created October 4, 2018 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hg-pyun/c8fc0b1e058afd51343d6ed077818da0 to your computer and use it in GitHub Desktop.
Save hg-pyun/c8fc0b1e058afd51343d6ed077818da0 to your computer and use it in GitHub Desktop.
iterator.01.js
/* 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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment