Array Object In TypeScript: Part 3

Array Object In TypeScript: Part 3 

 
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.
 

Push() Method

 
In TypeScript the push() method inserts one or more elements at the last position in an array. It returns the new length of the array. The push() method treats an array as a stack.
 
Syntax
  1. array.push(element1,element 2,.........)  
Function
  1. Push() {  
  2.  var fstarry: string[] = ['C''Sharp''Corner'];  
  3.  var elemnt = prompt("Enter a string for push in array");  
  4.  fstarry.push(elemnt);  
  5.  var span = document.createElement("span");  
  6.  span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";  
  7.  document.body.appendChild(span);  
  8. }  

Pop() Method

 
In TypeScript, the pop() method is the most popular method of an Array object. The pop() method removes the last element in the array, decrements the length, and also returns the element that is removed. The pop() method will return Null if the array is empty.
 
Syntax
  1. array.pop()  
Function
  1. Pop() {  
  2.  var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  3.  var index = arrayName.pop().toString();  
  4.  var span = document.createElement("span");  
  5.  span.style.color = "#7a1111";  
  6.  span.innerText = "Pop Method \n pop value from Array -> " + index;  
  7.  document.body.appendChild(span);  
  8. }  

Complete Program

 
Push_Pop.ts
  1. class Push_Pop {  
  2.   Push() {  
  3.     var fstarry: string[] = ['C''Sharp''Corner'];  
  4.     var elemnt = prompt("Enter a string for push in array");  
  5.     fstarry.push(elemnt);  
  6.     var span = document.createElement("span");  
  7.     span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";  
  8.     document.body.appendChild(span);  
  9.   }  
  10.   Pop() {  
  11.     var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  12.     var index = arrayName.pop().toString();  
  13.     var span = document.createElement("span");  
  14.     span.style.color = "#7a1111";  
  15.     span.innerText = "Pop Method \n pop value from Array -> " + index;  
  16.     document.body.appendChild(span);  
  17.   }  
  18. }  
  19. window.onload = () => {  
  20.   var obj = new Push_Pop();  
  21.   obj.Push();  
  22.   obj.Pop();  
  23. };   
Push_Pop_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="Push_Pop.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Push() and Pop() Array Method In TypeScript</h3>  
  12.     </body>  
  13. </html>  
Push_Pop.js
  1. var Push_Pop = (function() {  
  2.  function Push_Pop() {}  
  3.  Push_Pop.prototype.Push = function() {  
  4.   var fstarry = ['C''Sharp''Corner'];  
  5.   var elemnt = prompt("Enter a string for push in array");  
  6.   fstarry.push(elemnt);  
  7.   var span = document.createElement("span");  
  8.   span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";  
  9.   document.body.appendChild(span);  
  10.  };  
  11.  Push_Pop.prototype.Pop = function() {  
  12.   var arrayName = ['C''Sharp''Corner''VB''Net''Heaven'];  
  13.   var index = arrayName.pop().toString();  
  14.   var span = document.createElement("span");  
  15.   span.style.color = "#7a1111";  
  16.   span.innerText = "Pop Method \n pop value from Array -> " + index;  
  17.   document.body.appendChild(span);  
  18.  };  
  19.  return Push_Pop;  
  20. })();  
  21. window.onload = function() {  
  22.  var obj = new Push_Pop();  
  23.  obj.Push();  
  24.  obj.Pop();  
  25. };  
Output 1
 
Enter any string to be inserted into the array:
 
enter-value-for-push.gif
 
Output 2
 
final-result.gif


Similar Articles