Array Object In TypeScript: Part 5

Array Object In TypeScript: Part 5 

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

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
  1. array.slice(start,end)   
Function
  1. Slice() {  
  2.  var fstarry: string[] = ['C''Sharp''Corner''Dot''Net''Heaven''Modeling''Corner'];  
  3.  var sliceArry = fstarry.slice(3, 7);  
  4.  var span = document.createElement("span");  
  5.  span.innerText = "Slice Method \n After Reverse array is -> " + sliceArry + "\n";  
  6.  document.body.appendChild(span);  
  7. }  

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
  1. 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
  1. Sort() {  
  2.  var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  3.  var sortArry = arrayName.sort();  
  4.  var span = document.createElement("span");  
  5.  span.style.color = "blueviolet";  
  6.  span.innerText = "Sort Method \n After sorting array is -> " + sortArry + "\n";  
  7.  document.body.appendChild(span);  
  8. }  

Complete Program

 
Slice_Sort.ts
  1. class Slice_Sort {  
  2.  Slice() {  
  3.   var fstarry: string[] = ['C''Sharp''Corner''Dot''Net''Heaven''Modeling''Corner'];  
  4.   var sliceArry = fstarry.slice(3, 7);  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "Slice Method \n After Reverse array is -> " + sliceArry + "\n";  
  7.   document.body.appendChild(span);  
  8.  }  
  9.  Sort() {  
  10.   var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  11.   var sortArry = arrayName.sort();  
  12.   var span = document.createElement("span");  
  13.   span.style.color = "blueviolet";  
  14.   span.innerText = "Sort Method \n After sorting array is -> " + sortArry + "\n";  
  15.   document.body.appendChild(span);  
  16.  }  
  17. }  
  18. window.onload = () => {  
  19.  var obj = new Slice_Sort();  
  20.  obj.Slice();  
  21.  obj.Sort();  
  22. };  
Slice_Sort_MethodDemo.htm
  1. <!DOCTYPE html>  
  2. <html lang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta charset="utf-8" />  
  6.         <title>TypeScript HTML App</title>  
  7.         <link rel="stylesheet" href="app.css" type="text/css" />  
  8.         <script src="Slice_Sort.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Slice() and Sort() Array Method In TypeScript</h3>  
  12.     </body>  
  13. </html>  
Slice_Sort.js
  1. var Slice_Sort = (function() {  
  2.  function Slice_Sort() {}  
  3.  Slice_Sort.prototype.Slice = function() {  
  4.   var fstarry = ['C''Sharp''Corner''Dot''Net''Heaven''Modeling''Corner'];  
  5.   var sliceArry = fstarry.slice(3, 7);  
  6.   var span = document.createElement("span");  
  7.   span.innerText = "Slice Method \n After Reverse array is -> " + sliceArry + "\n";  
  8.   document.body.appendChild(span);  
  9.  };  
  10.  Slice_Sort.prototype.Sort = function() {  
  11.   var arrayName = ['C''Sharp''Corner''VB''Net''Heaven'];  
  12.   var sortArry = arrayName.sort();  
  13.   var span = document.createElement("span");  
  14.   span.style.color = "blueviolet";  
  15.   span.innerText = "Sort Method \n After sorting array is -> " + sortArry + "\n";  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  return Slice_Sort;  
  19. })();  
  20. window.onload = function() {  
  21.  var obj = new Slice_Sort();  
  22.  obj.Slice();  
  23.  obj.Sort();  
  24. };  
Output
 
Result.gif


Similar Articles