Array Object In TypeScript: Part 5

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

  4. reverse() and shift() 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. TypeScript provides many methods for the array object.

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

Slice() Method

In TypeScript the slice() method returns a new array that starts with the index given by the first parameter and continuing for the number of elements given by the second parameter. The slice and concat methods let you create a new array from part of or all of an array. In either case, the original array is not modified. If you want the original array to be replaced by the new array, you can set the old array equal to the new array.

Syntax

array.slice(start,end)

Function

Slice()

    {

        var fstarry: string[] = ['C','Sharp','Corner','Dot','Net','Heaven','Modeling','Corner']; 

        var sliceArry=fstarry.slice(3, 7);

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

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

        document.body.appendChild(span);

    }

 

 

Sort() Method

In TypeScript, it is used to sort the array. When no parameter is passed, this method sorts the elements in an array into ascending alphanumeric sequence.

Syntax

array.sort(comparison_function)

comparison_function: is an optional parameter. It changes the default order of the sort method. You can pass the name of a comparison function to the method. This function should receive two parameters and return a positive value if the first parameter is greater than the second, and returns Zero if the two parameters are equal and returns a negative value if the first parameter is less than the second parameter.


F
unction

 

Sort()

    {

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

        var sortArry = arrayName.sort();

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

        span.style.color = "blueviolet";

        span.innerText = "Sort Method \n After sorting array is -> " + sortArry+"\n";

        document.body.appendChild(span);

    }

 

Complete Program

Slice_Sort.ts

class Slice_Sort

{

    Slice()

    {

        var fstarry: string[] = ['C','Sharp','Corner','Dot','Net','Heaven','Modeling','Corner']; 

        var sliceArry=fstarry.slice(3, 7);

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

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

        document.body.appendChild(span);

    }

    Sort()

    {

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

        var sortArry = arrayName.sort();

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

        span.style.color = "blueviolet";

        span.innerText = "Sort Method \n After sorting array is -> " + sortArry+"\n";

        document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new Slice_Sort();

    obj.Slice();

    obj.Sort();

};

 

Slice_Sort_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="Slice_Sort.js"></script>

</head>

<body>

    <h3>Slice() and Sort() Array Method In TypeScript</h3>

</body>

</html>

 

Slice_Sort.js

var Slice_Sort = (function () {

    function Slice_Sort() { }

    Slice_Sort.prototype.Slice = function () {

        var fstarry = [

            'C',

            'Sharp',

            'Corner',

            'Dot',

            'Net',

            'Heaven',

            'Modeling',

            'Corner'

        ];

        var sliceArry = fstarry.slice(3, 7);

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

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

        document.body.appendChild(span);

    };

    Slice_Sort.prototype.Sort = function () {

        var arrayName = [

            'C',

            'Sharp',

            'Corner',

            'VB',

            'Net',

            'Heaven'

        ];

        var sortArry = arrayName.sort();

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

        span.style.color = "blueviolet";

        span.innerText = "Sort Method \n After sorting array is -> " + sortArry + "\n";

        document.body.appendChild(span);

    };

    return Slice_Sort;

})();

window.onload = function () {

    var obj = new Slice_Sort();

    obj.Slice();

    obj.Sort();

};

//@ sourceMappingURL=Slice_Sort.js.map

  


Output

Result.gif

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