Instantly share code, notes, and snippets.

Embed
What would you like to do?
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