Array Object In TypeScript: Part 4

Before reading this article, please go through the following articles:

  1. concat() and lastIndexOf() array object method in typescript

  2. join() and lastIndexOf() array object method in typescript

  3. push() and pop() array object method in typescript

Introduction

In TypeScript, the array object makes our work easier. The TypeScript
Array object is used to store multiple values in a single variable at a time. TypeScript provides many methods.

In this article I am describing some of the TypeScript array methods.

Reverse() Method

In TypeScript, reverse() is used to reverse the order of the element in the array. The reverse() method has no parameters and returns the array with the values reversed. In the reverse() method, the last element of the array becomes first element and the first element of the array becomes the last element.

Syntax

array.reverse()

Function

Reverse()

    {

        var fstarry: string[] = ['C','Sharp','Corner']; 

        fstarry.reverse();

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

        span.innerText = "Revrese Method \n After Reverse array is -> " + fstarry + "\n";

        document.body.appendChild(span);

    }

 

Shift() Method

In TypeScript the shift() method is used to remove the first element in the array, decrement the array length and return the removed element. If the keys are numeric then all elements will get new keys, starting from 0 and increasing by 1.

Syntax

array.shift()


F
unction

 

Shift()

    {

        var arrayName: string[] = ['C','Sharp','Corner','VB','Net','Heaven'];

        var index = arrayName.shift().toString();

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

        span.style.color = "#7a1111";

        span.innerText = "Shift Method \n Shift Element is -> " + index;

        document.body.appendChild(span);

    }


Complete Program

Reverse_Shift.ts

class Reverse_Shift

{

    Reverse()

    {

        var fstarry: string[] = ['C','Sharp','Corner']; 

        fstarry.reverse();

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

        span.innerText = "Revrese Method \n After Reverse array is -> " + fstarry + "\n";

        document.body.appendChild(span);

    }

    Shift()

    {

        var arrayName: string[] = ['C','Sharp','Corner','VB','Net','Heaven'];

        var index = arrayName.shift().toString();

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

        span.style.color = "#7a1111";

        span.innerText = "Shift Method \n Shift Element is -> " + index;

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new Reverse_Shift();

    obj.Reverse();

    obj.Shift();

};


Reverse_Shift_MethodDemo.htm

<!DOCTYPE html>

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

<head>

    <meta charset="utf-8" />

    <title>TypeScript HTML App</title>

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

    <script src="Reverse_Shift.js"></script>

</head>

<body>

    <h3>Reverse() and Shift() Array Method In TypeScript</h3>

</body>

</html>

Reverse_Shift.js

var Reverse_Shift = (function () {

    function Reverse_Shift() { }

    Reverse_Shift.prototype.Reverse = function () {

        var fstarry = [

            'C',

            'Sharp',

            'Corner'

        ];

        fstarry.reverse();

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

        span.innerText = "Revrese Method \n After Reverse array is -> " + fstarry + "\n";

        document.body.appendChild(span);

    };

    Reverse_Shift.prototype.Shift = function () {

        var arrayName = [

            'C',

            'Sharp',

            'Corner',

            'VB',

            'Net',

            'Heaven'

        ];

        var index = arrayName.shift().toString();

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

        span.style.color = "#7a1111";

        span.innerText = "Shift Method \n Shift Element is -> " + index;

        document.body.appendChild(span);

    };

    return Reverse_Shift;

})();

window.onload = function () {

    var obj = new Reverse_Shift();

    obj.Reverse();

    obj.Shift();

};

//@ sourceMappingURL=Reverse_Shift.js.map

 

 

Output

final-result.gif

 

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all