Array Object In TypeScript: Part 3

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

 

Introduction

In TypeScript, the array object makes our work easier. The TypeScript
Array object stores 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.

Push() Method

In TypeScript the push() method inserts one or more elements at the last position in an array. It returns the new length of the array. The push() method treats an array as a stack.

Syntax

array.push(element1,element 2,.........)

Function

Push()

    {

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

        var elemnt = prompt("Enter a string for push in array");

        fstarry.push(elemnt);

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

        span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";

        document.body.appendChild(span);

    }

 

 

Pop() Method

In TypeScript, the pop() method is the most popular method of an Array object. The pop() method removes the last element in the array, decrements the length and also returns the element that it removed. The pop() method will return Null if the array is empty.

Syntax

array.pop()


F
unction

Pop()

    {

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

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

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

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

        span.innerText = "Pop Method \n pop value from Array -> " + index;

        document.body.appendChild(span);

    }

 

Complete Program

Push_Pop.ts

class Push_Pop

{

    Push()

    {

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

        var elemnt = prompt("Enter a string for push in array");

        fstarry.push(elemnt);

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

        span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";

        document.body.appendChild(span);

    }

    Pop()

    {

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

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

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

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

        span.innerText = "Pop Method \n pop value from Array -> " + index;

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new Push_Pop();

    obj.Push();

    obj.Pop();

};

 

Push_Pop_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="Push_Pop.js"></script>

</head>

<body>

    <h3>Push() and Pop() Array Method In TypeScript</h3>

</body>

</html>

 

Push_Pop.js

var Push_Pop = (function () {

    function Push_Pop() { }

    Push_Pop.prototype.Push = function () {

        var fstarry = [

            'C',

            'Sharp',

            'Corner'

        ];

        var elemnt = prompt("Enter a string for push in array");

        fstarry.push(elemnt);

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

        span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";

        document.body.appendChild(span);

    };

    Push_Pop.prototype.Pop = function () {

        var arrayName = [

            'C',

            'Sharp',

            'Corner',

            'VB',

            'Net',

            'Heaven'

        ];

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

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

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

        span.innerText = "Pop Method \n pop value from Array -> " + index;

        document.body.appendChild(span);

    };

    return Push_Pop;

})();

window.onload = function () {

    var obj = new Push_Pop();

    obj.Push();

    obj.Pop();

};

//@ sourceMappingURL=Push_Pop.js.map

 

 

Output 1

Enter any string to be inserted into the array:


enter-value-for-push.gif


Output 2


final-result.gif

 

 

Up Next
    Ebook Download
    View all

    Test

    Read by 16 people
    Download Now!
    Learn
    View all