Iterators

ES 2015 provided built-in iterators for the collection types String, Array, Map and Set, so you don't have to create iterators for these objects.
If you have a custom type and want to make it iterable so that you can use the for...of loop construct, you need to implement the iteration protocols.


Iterator protocol

The [Symbol.iterator] is one of the built-in well-known symbols in ES6.
An object is an iterator when it implements an interface (or API) that answers two questions:
Is there any element left?
If there is, what is the element?


An object is qualified as an iterator when it has a next() method that returns an object with two properties:
done: a boolean value indicating whether or not there are any more elements that could be iterated upon.
value: the current element.


Each time you call the next(), it returns the next value in the collection:
If you call the next() method after the last value has been returned, the next() returns the result object as follows:
The value of the done property indicates that there is no more value to return and the value of the property is set to undefined.


Declares an array called "marks" and retrieves an iterator object

   let marks = [10,20,30] 
   let iter = marks[Symbol.iterator]();
   console.log(iter.next()) // {value: 10, done: false}
   console.log(iter.next()) // {value: 20, done: false}
   console.log(iter.next()) // {value: 30, done: false}
   console.log(iter.next()) // {value: undefined, done: true}

Custom Iterable

JavaScript types which are not iterable by default can be iterated by using the iterable protocol.

   class CustomerList { 
      constructor(customers){
         //adding customer objects to an array
         this.customers = [].concat(customers)
      }
      //implement iterator function
      [Symbol.iterator](){
         let count=0;
         let customers = this.customers
         return {
            next:function(){
            //retrieving a customer object from the array
               let customerVal = customers[count];
               count+=1;
               if(count<=customers.length){
                  return {
                     value:customerVal,
                     done:false
                  }
               }
               //return true if all customer objects are iterated
               return {done:true}
            }
         }
      }
   }
   //create customer objects
   let c1={
      firstName:'Sachin',
      lastName:'Tendulkar'
   }
   let c2={
      firstName:'Rahul',
      lastName:'Dravid'
   }
   //define a customer array and initialize it let customers=[c1,c2]
   //pass customers to the class' constructor
   let customersObj = new CustomerList(customers);
   //iterating using for..of
   for(let c of customersObj){
      console.log(c)
   }
   //iterating using the next() method
   let iter = customersObj[Symbol.iterator]();
   console.log(iter.next())
   console.log(iter.next())
   console.log(iter.next())


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext