Array Object In TypeScript: Part 1

Introduction

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

Concat() Method

 
In TypeScript the concat() method is used to concatenate or combine two or more arrays and return a new array. It does not change the existing arrays, it returns a new array, which contains the values of both arrays.
 
Syntax
  1. array.concat(value1, value2, valu3, ....................valuen)  
array: The Array object for which all other arrays are concatenated.
value: The values are array items to add to the end of the array.
 
Function
  1. Concat() {  
  2.  var fstarry: string[] = ['C''Sharp'];  
  3.  var scndarry: string[] = ['Corner''.com'];  
  4.  var result = fstarry.concat(scndarry);  
  5.  var span = document.createElement("span");  
  6.  span.innerText = "Concat Method \n First Array is -> " + fstarry + "\n" + "Second Array is -> " + scndarry + "\n" + "Concatenated Array is -> " + result + "\n";  
  7.  document.body.appendChild(span);  
  8. }  

IndexOf() Method

 
In TypeScript the indexOf() method is used to search for a specified value's index number from the array. In the indexOf() method, the search value parameter will a specific position to start from.
 
If the position is not set then the search will start from the beginning. The beginning position starts from 0. After the search it will return an index number of values; if there is not a match then it will return -1.
 
Syntax
  1. array.indexOf(searchvalue, start)  
Function
  1. IndexOf() {  
  2.  var arrayName: string[] = ['C''Sharp''Corner''Dot''Net''Heaven'];  
  3.  var index = arrayName.indexOf('Dot');  
  4.  var span = document.createElement("span");  
  5.  span.style.color = "green";  
  6.  span.innerText = "IndexOf Method \n Index of (Dot) in array is -> " + index;  
  7.  document.body.appendChild(span);  
  8. }  

Complete Program

 
Concat_IndexOf.ts
  1. class Concat_IndexOf  
  2. {  
  3.  Concat()  
  4.  {  
  5.   var fstarry: string[] = ['C''Sharp'];  
  6.   var scndarry: string[] = ['Corner''.com'];  
  7.   var result = fstarry.concat(scndarry);  
  8.   var span = document.createElement("span");  
  9.   span.innerText = "Concat Method \n First Array is -> " + fstarry + "\n" + "Second Array is -> " + scndarry + "\n" + "Concatenated Array is -> " + result + "\n";  
  10.   document.body.appendChild(span);  
  11.  }  
  12.  IndexOf()  
  13.  {  
  14.   var arrayName: string[] = ['C''Sharp''Corner''Dot''Net''Heaven'];  
  15.   var index = arrayName.indexOf('Dot');  
  16.   var span = document.createElement("span");  
  17.   span.style.color = "green";  
  18.   span.innerText = "IndexOf Method \n Index of (Dot) in array is -> " + index;  
  19.   document.body.appendChild(span);  
  20.  }  
  21. }  
  22. window.onload = () =>  
  23.  { 
  24.   var obj = new Concat_IndexOf();  
  25.   obj.Concat();  
  26.   obj.IndexOf();  
  27.  };  
Concat_IndexOf_MethodDemo.htm
  1. < !DOCTYPE html >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  script src = "Concat_IndexOf.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h3 > Concat() and IndexOf() Array Method In TypeScript < /h3>  
  23.  <  
  24.  /body>  
  25.  <  
  26.  /html>  
Concat_IndexOf.js
  1. var Concat_IndexOf = (function() {  
  2.  function Concat_IndexOf() {}  
  3.  Concat_IndexOf.prototype.Concat = function() {  
  4.   var fstarry = ['C''Sharp'];  
  5.   var scndarry = ['Corner''.com'];  
  6.   var result = fstarry.concat(scndarry);  
  7.   var span = document.createElement("span");  
  8.   span.innerText = "Concat Method \n First Array is -> " + fstarry + "\n" + "Second Array is -> " + scndarry + "\n" + "Concatenated Array is -> " + result + "\n";  
  9.   document.body.appendChild(span);  
  10.  };  
  11.  Concat_IndexOf.prototype.IndexOf = function() {  
  12.   var arrayName = ['C''Sharp''Corner''Dot''Net''Heaven'];  
  13.   var index = arrayName.indexOf('Dot');  
  14.   var span = document.createElement("span");  
  15.   span.style.color = "green";  
  16.   span.innerText = "IndexOf Method \n Index of (Dot) in array is -> " + index;  
  17.   document.body.appendChild(span);  
  18.  };  
  19.  return Concat_IndexOf;  
  20. })();  
  21. window.onload = function() {  
  22.  var obj = new Concat_IndexOf();  
  23.  obj.Concat();  
  24.  obj.IndexOf();  
  25. };  
Output
 
fincal-output.gif


Similar Articles