TypeScript Iterators and Generators


TypeScript has some built in types like Array, String, Int32Array, Uint32Array, etc. support iteration because they have implemented Symbol.iterator property. In order to perform iteration on custom object, object must implement Symbol.iterator property.

For..Of Statement

The for..of loop work with iterable object like array by invoking System. Iterator property and return value of iterable objects. For Example:

class Customer {
	Display():number
	{
       let numbers = [1, 2, 3,4,5,6,7,8,9,10];
        var s : any = "";
        var o = 0;
         for (let num of numbers) {
          s = s + "\n Array Value is - " + num;
         }
        return s;    
	}
}

Live Demo

For..In Statement

The for..of loop work with any iterable object and return key of iterable objects. For Example:

class Customer {
	Display():number
	{
       let numbers = [1, 2, 3,4,5,6,7,8,9,10];
        var s : any = "";
        var o = 0;
         for (let num in numbers) {
          s = s + "\n Array Value is - " + num;
         }
        return s;    
	}
}

Live Demo

Difference between For...Of and For...In

  • For..In - returns a list of keys on the iterable object
  • For..In - operates on any object
  • For..Of - returns a list of value on the iterable object
  • For..Of - operates on any object that implement Symbol.iterator property

See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives



 
SHARE THIS IF YOU LIKE!
submit to reddit

TYPESCRIPT LANGUAGE TECHNIQUES

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.