![]() ![]() The good thing with generators is that they are lazy and can take another generator's result as an argument. )Ĭonsole.log('iterate values and indexes from a generator')įor (const of eachWithIndex(eachFromTo(10, 13))) console.log(val, i)Ĭonst anArray = eachToArray(eachFromTo(10, 13))Ĭonsole.log('iterate values and indexes from an array')įor (const of eachWithIndex(anArray)) console.log(val, i) yields every value and index of an iterable (array, generator. In this loop, the variable i automatically receives the index so that each character can be. convers an iterable to an array (potential infinite loop)įor (const val of iterable) result.push(val) This is different from the ordinary for loop that we studied before. Or ES6’s, which now has support across current browser versions: for (const of myArray.entries()) For them, there’s ES5’s forEach method that passes both the value and the index to the function you give it: var myArray = You shouldn’t use it to iterate over arrays. For…in iterates over property names, not values, and does so in an unspecified order (yes, even after ES6). ![]()
0 Comments
Leave a Reply. |