Skip to content

Instantly share code, notes, and snippets.

@abstractmachines
Last active November 2, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstractmachines/fb010c0385054237f8b04ae158aed873 to your computer and use it in GitHub Desktop.
Save abstractmachines/fb010c0385054237f8b04ae158aed873 to your computer and use it in GitHub Desktop.
JavaScript Iterators: Strings, spread, and for of
/* 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