Array Object In TypeScript: Part 2

Array Object In TypeScript: Part 2 

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

Introduction

 
In TypeScript, the array object makes 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 the TypeScript array methods Join and lastIndexOf.
 

Join() Method

 
In TypeScript the join() method is used to join the elements of an array into a string and return the string. The join() method takes a separator as a parameter that specifies the special character like space, comma and plus or any other character to separate each and every element of the array. When no parameter is passed, this method converts all the elements of the array to strings and concatenates them separated by commas.
 
Syntax
  1. array.join(separator)  
Function
  1. Join() {  
  2.  var fstarry: string[] = ['C''Sharp''Corner''.com'];  
  3.  var result = fstarry.join();  
  4.  var result1 = fstarry.join('+');  
  5.  var result2 = fstarry.join('*');  
  6.  var span = document.createElement("span");  
  7.  span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" + "Third array sample join with (*) -> " + result2 + "\n";  
  8.  document.body.appendChild(span);  
  9. }  

LastIndexOf() Method

  
In TypeScript the lastIndexOf() method searches for the last occurrence of a string value from the given characters. In the lastIndexOf() method the searchvalue parameter will start from a specified position.
 
If the position is not set then the search will start from the beginning. The beginning position starts at 0. After searching it will return the index number of values. If it does not find a match then it will return -1. The lastIndexOf() method is case sensitive.
 
Syntax
  1. array.lastIndexOf(searchvalue,start)  
Function
  1. LastIndexOf() {  
  2.  var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  3.  var index = arrayName.lastIndexOf('Corner');  
  4.  var span = document.createElement("span");  
  5.  span.style.color = "#7a1111";  
  6.  span.innerText = "LastIndexOf Method \n LastIndexOf of (Corner) in array is -> " + index;  
  7.  document.body.appendChild(span);  
  8. }  

Complete Program

 
Join_LastIndexOf.ts
  1. class Join_LastIndexOf  
  2. {  
  3.  Join()  
  4.  {  
  5.   var fstarry: string[] = ['C''Sharp''Corner''.com'];  
  6.   var result = fstarry.join();  
  7.   var result1 = fstarry.join('+');  
  8.   var result2 = fstarry.join('*');  
  9.   var span = document.createElement("span");  
  10.   span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" + "Third array sample join with (*) -> " + result2 + "\n";  
  11.   document.body.appendChild(span);  
  12.  }  
  13.  LastIndexOf()  
  14.  {  
  15.   var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  16.   var index = arrayName.lastIndexOf('Corner');  
  17.   var span = document.createElement("span");  
  18.   span.style.color = "#7a1111";  
  19.   span.innerText = "LastIndexOf Method \n LastIndexOf of (Corner) in array is -> " + index;  
  20.   document.body.appendChild(span);  
  21.  }  
  22. }  
  23. window.onload = () =>  
  24.  {  
  25.   var obj = new Join_LastIndexOf();  
  26.   obj.Join();  
  27.   obj.LastIndexOf();  
  28.  };  
Join_LastIndexOf_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="Join_LastIndexOf.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Join() and LastIndexOf() Array Method In TypeScript</h3>  
  12.     </body>  
  13. </html>  
Join_LastIndexOf.js
  1. var Join_LastIndexOf = (function() {  
  2.  function Join_LastIndexOf() {}  
  3.  Join_LastIndexOf.prototype.Join = function() {  
  4.   var fstarry = [  
  5.    'C',  
  6.    'Sharp',  
  7.    'Corner',  
  8.    '.com'  
  9.   ];  
  10.   var result = fstarry.join();  
  11.   var result1 = fstarry.join('+');  
  12.   var result2 = fstarry.join('*');  
  13.   var span = document.createElement("span");  
  14.   span.innerText = "Join Method \n First array Sample join as simple -> " + result + "\n" + "Second array sample join with plus (+) -> " + result1 + "\n" +  
  15.    "Third array sample join with (*) -> " + result2 + "\n";  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  Join_LastIndexOf.prototype.LastIndexOf = function() {  
  19.   var arrayName = [  
  20.    'C',  
  21.    'Sharp',  
  22.    'Corner',  
  23.    'VB',  
  24.    'Net',  
  25.    'Heaven'  
  26.   ];  
  27.   var index = arrayName.lastIndexOf('Corner');  
  28.   var span = document.createElement("span");  
  29.   span.style.color = "#7a1111";  
  30.   span.innerText = "LastIndexOf Method \n LastIndexOf of (Dot) in array is -> " + index;  
  31.   document.body.appendChild(span);  
  32.  };  
  33.  return Join_LastIndexOf;  
  34. })();  
  35. window.onload = function() {  
  36.  var obj = new Join_LastIndexOf();  
  37.  obj.Join();  
  38.  obj.LastIndexOf();  
  39. };  
  40. //@ sourceMappingURL=Join_LastIndexOf.js.map  
Output
 
result.gif


Similar Articles