For-In Statement With Array in TypeScript

For-in statement with array in TypeScript

A for-in statement makes it easier to work with an array. The for-in statement doesn't require a separate expression that initializes, tests and increments an index counter like a for loop does. Instead, you declare a variable that will be used to refer to the index of each element in the array. Then, within the loop, you can use this variable to access each element in the array.

This loop works primarily with arrays. It allows us to loop through each element in the array, without having to know how many elements the array actually contains. In other words, "For each element in the array, execute some code." rather than having to work out the index number of each element, the for..in loop does it for us, and automatically moves to the next index with each iteration.

Syntax

The syntax for use with an array is:

for (Index in arrayName)

{

  //statement or block to execute

}

The following example prints the weekdays using a for-in statement with an array. Let's use the following.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. Give the name of your application as "for-in-array" and then click ok.

Step 2

After this session the project has been created. A new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file and html file.

Coding

for-in-array.ts

class forinarray

{

    start()

    {

       var days: string[]= [ "Sunday","Monday","TuesDay","Wednesday","Thursday","Friday","Saturday" ];

       var d:string;

       for (d in days)

       {      

            var span = document.createElement("span");

            span.innerText = days[d]+ "\n";

            document.body.appendChild(span); 

       }     

    }

}

window.onload = () =>

{

    var greeter = new forinarray();

    greeter.start();

};

 

Demo.html

<!DOCTYPEhtml>

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>TypeScript HTML App</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <h2>for-in statement with array in TypeScript</h2>

    <h3>Week Days</h3>

    <divid="content"/>

</body>

</html>

 

app.js

var forinarray = (function () {

    function forinarray() { }

    forinarray.prototype.start = function () {

        var days = [

            "Sunday",

            "Monday",

            "TuesDay",

            "Wednesday",

            "Thursday",

            "Friday",

            "Saturday"

        ];

        var d;

        for(din days) {

            var span = document.createElement("span");

            span.innerText = days[d] + "\n";

            document.body.appendChild(span);

        }

    };

    return forinarray;

})();

window.onload = function () {

    var greeter = new forinarray();

    greeter.start();

};

 

Output


final-result.jpg

 

Reference By

http://www.typescriptlang.org/

Up Next
    Ebook Download
    View all
    Test
    Read by 16 people
    Download Now!
    Learn
    View all