Array Object In TypeScript: Part 6

Array Object In TypeScript: Part 6 

 
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 at a time. TypeScript provides many methods.
 
In this article, I am describing some of the TypeScript array methods splice and toString.
 

Splice() Method

 
In TypeScript the splice() method removes selected elements from an array and replaces them with new elements. A simple splice method without elements_list parameter removes the number of elements given by the second parameter starting with the index given by the first parameter. A splice method with an elements_list parameter removes the number of elements given by the second parameter starting with the index given by the first parameter then replaces those elements with the ones given by the third parameter. Both methods return the removed elements.
 
Syntax
  1. array.splice(start,number,elements_list)  
start is a required parameter. It is a numeric value. It specifies where the function will start removing elements.
 
number is an optional parameter. It determines how many elements will be removed, and also is the length of the array returned.
 
Elements_list is an optional parameter. It specifies an array with the elements that will be inserted into the original array.
 
Function
  1. Splice() {  
  2.  var fstarry: string[] = ['C''Sharp''Corner''Dot''Net''Heaven''Modeling''Corner'];  
  3.  var removeelemet = fstarry.splice(2, 1, 'Nitin').toString();  
  4.  var span = document.createElement("span");  
  5.  span.innerText = "Splice Method \n Remove element is -> " + removeelemet + "\n" + "After add element, Array is -> " + fstarry + "\n";  
  6.  document.body.appendChild(span);  
  7. }  

toString() Method

 
In TypeScript the toString() method is used to convert an array to a string and return the string. The toString() method is the same as the join method without any parameters passed to it.
 
Syntax
  1. array.toString()  
Function
  1. toString() {  
  2.  var arrayString: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  3.  arrayString.toString();  
  4.  var span = document.createElement("span");  
  5.  span.style.color = "blueviolet";  
  6.  span.innerText = "toString Method \n Array is -> " + arrayString + "\n";  
  7.  document.body.appendChild(span);  
  8. }  

Complete Program

 
Splice_toString.ts
  1. class Splice_toString {  
  2.  Splice() {  
  3.   var fstarry: string[] = ['C''Sharp''Corner''Dot''Net''Heaven''Modeling''Corner'];  
  4.   var removeelemet = fstarry.splice(2, 1, 'Nitin').toString();  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "Splice Method \n Remove element is -> " + removeelemet + "\n" + "After add element, Array is -> " + fstarry + "\n";  
  7.   document.body.appendChild(span);  
  8.  }  
  9.  toString() {  
  10.   var arrayString: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  11.   arrayString.toString();  
  12.   var span = document.createElement("span");  
  13.   span.style.color = "blueviolet";  
  14.   span.innerText = "toString Method \n Array is -> " + arrayString + "\n";  
  15.   document.body.appendChild(span);  
  16.  }  
  17. }  
  18. window.onload = () => {  
  19.  var obj = new Splice_toString();  
  20.  obj.Splice();  
  21.  obj.toString();  
  22. };  
Splice_toString_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="Splice_toString.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Splice() and toString() Array Method In TypeScript</h3>  
  12.     </body>  
  13. </html>  
Splice_toString.js
  1. var Splice_toString = (function() {  
  2.  function Splice_toString() {}  
  3.  Splice_toString.prototype.Splice = function() {  
  4.   var fstarry = ['C''Sharp''Corner''Dot''Net''Heaven''Modeling''Corner'];  
  5.   var removeelemet = fstarry.splice(2, 1, 'Nitin').toString();  
  6.   var span = document.createElement("span");  
  7.   span.innerText = "Splice Method \n Remove element is -> " + removeelemet + "\n" + "After add element, Array is -> " + fstarry + "\n";  
  8.   document.body.appendChild(span);  
  9.  };  
  10.  Splice_toString.prototype.toString = function() {  
  11.   var arrayString = ['C''Sharp''Corner''VB''Net''Heaven'];  
  12.   arrayString.toString();  
  13.   var span = document.createElement("span");  
  14.   span.style.color = "blueviolet";  
  15.   span.innerText = "toString Method \n Array is -> " + arrayString + "\n";  
  16.   document.body.appendChild(span);  
  17.  };  
  18.  return Splice_toString;  
  19. })();  
  20. window.onload = function() {  
  21.  var obj = new Splice_toString();  
  22.  obj.Splice();  
  23.  obj.toString();  
  24. };  
Output
 
Result.gif