Last active
November 2, 2017 23:15
-
-
Save abstractmachines/fb010c0385054237f8b04ae158aed873 to your computer and use it in GitHub Desktop.
JavaScript Iterators: Strings, spread, and for of
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* JavaScript Iterators | |
Strings as data source; for of and spread operator as data consumers | |
Strings can be consumed as iterables using the spread operator. | |
Note that strings will be parsed according to code points, not by character, | |
and so num characters parsed will be somewhat nondeterministic. | |
Sources: | |
1 - Axel Rauschmeyer: http://exploringjs.com/es6/ch_iteration.html | |
2 - http://www.zsoltnagy.eu/es6-iterators-and-generators-in-practice/ | |
*/ | |
let album = [...'OK Computer']; | |
let letterz = album.entries(); | |
for( let letter of letterz ) { | |
console.log( letter ); | |
} | |
/* output: | |
[0,"O"] | |
[1,"K"] | |
[2," "] | |
[3,"C"] | |
[4,"o"] | |
[5,"m"] | |
[6,"p"] | |
[7,"u"] | |
[8,"t"] | |
[9,"e"] | |
[10,"r"] | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment