Skip to main content

Unpractical use cases - Iterators

**

enter image description hereUnpractical use cases - Iterators

What is an iterator? Array Iterator Objects

An Array Iterator is an object, that represents a specific iteration over some specific Array instance object. There is not a named constructor for Array Iterator objects. Instead, Array iterator objects are created by calling certain methods of Array instance objects.

ref : cma-262

Instead of trying to understand it, lets just look at a example

enter image description here

Here we have an object that contains an array, to use the iterator on the array you can simply do something like

enter image description here

To use the iterator to move to the next item in the array you call .next()

enter image description here

console.log(theArrayIterator.next()) // x 5

{ value: 'all', done: false }    
{ value: 'the', done: false }    
{ value: 'array', done: false }    
{ value: 'values', done: false }
{ value: undefined, done: true }

As you can see, each time you call next, it prints the value of the next item in the array, and once it reaches the end of the array it returns done : true.

It is also possible to customize this behavior

enter image description here

The way this works can be found here Iteration_protocols. If we run the same code we did before

console.log(theArrayIterator.next()) // x 5
{ value: 'all', done: false, arrayIndex: '1 out of 4' }
{ value: 'the', done: false, arrayIndex: '2 out of 4' }
{ value: 'array', done: false, arrayIndex: '3 out of 4' }
{ value: 'values', done: false, arrayIndex: '4 out of 4' }
{ value: 'you have reached the end!', done: true }

The output now contains the arrayIndex property we added. While writing a twitter scraper with puppeteer I was trying to simulate a never ending twitter feed, so each time the feed would reach the last tweet in the batch, it would call automatically do the ajax call to get the new batch.

Iterators give you the ability to keep track in a very easy to use way and with a little bit of custom code you can greatly enhance that to do really cool stuff.

enter image description here

{ value: 'all', done: false, arrayIndex: '1 out of 4' }
{ value: 'the', done: false, arrayIndex: '2 out of 4' }    
{ value: 'right in the middle', done: false,    
test: 'so ummm just an update, halfway trough the list' }    
{ value: 'array', done: false, arrayIndex: '3 out of 4' }    
{ value: 'values', done: false, arrayIndex: '4 out of 4' }    
{ value: 'you have reached the end!',    
done: true,    
test:    
'this is just to show you can run a function when the iterator reaches the end oh yeah' }

I am sure there are a million ways to make this code better, but regardless I hope it kind of shows an unpractical way to use iterators.

Comments

Popular posts from this blog

Review : The new Pushtape Installation profile for Drupal 7 - Yay or nay?

AuthorRank: Google Have you heard of Pushtape?  Don't worry me neither.  But after this blog post we will definitely be sure if its a Drupal yay or nay.  The reason I chose this Installation profile as the theme for my post is simple, to make sites for bands in your community is an easy way to get your foot in the web development scene if you are just starting out.  All band sites basically works in the same way which means that you can just duplicate your site structure every time. OK so here we go. Pushtape is a new Installation Profile for Drupal 7 that focuses on musicians, it comes supplied with a discography framework, you can very easily post new events like shows, there is a custom photo gallery where you can upload multiple photos as a set and of course a nice looking news type blog for all those quick updates. The installation was really easy, clean, very well customized and quick. I use WebMatr...

Application development environment setup

Setup for application development right click and save images for bigger version Installing Java JAVA JDK Download In my experience I have found that starting your enviroment setup with java is best. In the system variables I made a new variable called it JAVA_HOME , and I set the value to: C:\Program Files\Java\jdk1.7.0_79 . After that, I edited PATH , which is also in the system variables and I added : ;%JAVA_HOME%\bin . In command prompt enter javac -version, if you see a return message javac 1.7.0_79 or something similar you can continue, I cant tell you how important it is to get this right, just know that things will go wrong at unexpected times. Installing Android SDK ANDROID SDK download List item In the system variables I made a new variable called it ANDROID_HOME , and I set the value to: C:\Program Files (x86)\Android\android-sdk . After that, I edited path , which is also in the system variables and I added : %ANDROID_HOME/tools% %ANDROID_HOM...