How To Use Associative Array in TypeScript

Associative Array in TypeScript

 
An array is an object that contains one or more items called elements. Each of these elements can be a primitive data type or an object. For instance, you can store numbers, strings, and Date objects in the same array.
 
Associative arrays provide another way to store information. An associative array is an array that uses string as the indexes. You can create a mixed array with numeric and string indexes within a single array. if you mix numeric and string indexes in an array then the length will indicate only the number of elements with numeric indexes. Associative arrays are very similar to numeric arrays in terms of functionality but they are different in terms of their index. In an associative array, each ID key is associated with a value.
 
Example
The following example describes how to use an associative array in TypeScript. In this code, we enter the employee name of the information you want. If the specified employee name is present in the array then it returns the employee information and if the employee name is not present in the array list then it shows the NO Information message. It is very simple and works as follows.
 
Step 1
Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window will then be opened. Specify a name for the application, like "Associative_Array", then click on the Ok button.
 
Step 2
After Step 1 your project has been created. The Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, CSS file, and HTML files.
 
 
Step 3
 
Associative.ts
  1. class Associative_array  
  2. {  
  3.  EmpInfo()  
  4.  {  
  5.   var information = new Array()  
  6.   information["Dinesh"] = "Dinesh Beniwal is Administrator of C# corner. His expert arears are ASP.NET, ADO.NET, HTML and PHP.";  
  7.   information["Mahesh"] = "Mahesh Chand is founder of C# Corner. Mahesh has been    
  8.   awarded prestigious Microsoft MVP Award  
  9.   for 7 consecutive years  
  10.   for his  
  11.   contributions to the developer community.  
  12.   ";  
  13.   information["Manish"] = "Manish is Very much interested in Microsoft & LifeStyle  
  14.   Accessory Designand working with Microsoft technologies.His expert areas are  
  15.   ASP.NET, ADO.NET, C#.NET and WPF.  
  16.   ";  
  17.   var info = prompt("Whose Employee information do you want?""");  
  18.   if ((info == "Dinesh") || (info == "Mahesh") || (info == "Manish"))  
  19.   {  
  20.    var y = document.createElement("y");  
  21.    y.innerText = information[info];  
  22.    document.body.appendChild(y);  
  23.   } else  
  24.    alert("No Information");  
  25.  }  
  26. }  
  27. window.onload = () =>  
  28.  {  
  29.   var greeter = new Associative_array();  
  30.   greeter.EmpInfo();  
  31.  };  
Demo.html
  1. < !DOCTYPEhtml >  
  2.  <  
  3.  htmllang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  metacharset = "utf-8" / >  
  9.  <  
  10.  title > Associative array < /title>  
  11.  <  
  12.  linkrel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  scriptsrc = "app.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h2 > Associative Array in TypeScript < /h2>  
  23.  <  
  24.  h3style = "color:#8a2323" > Employee Information < /h3>  
  25.  <  
  26.  divid = "content" / >  
  27.  <  
  28.  /body>  
  29.  <  
  30.  /html>  
app.js
  1. var Associative_array = (function() {  
  2.  function Associative_array() {}  
  3.  Associative_array.prototype.EmpInfo = function() {  
  4.   var information = new Array();  
  5.   information["Dinesh"] = "Dinesh Beniwal is Administrator of C# corner. His expert arears are ASP.NET, ADO.NET, HTML and PHP.";  
  6.   information["Mahesh"] = "Mahesh Chand is founder of C# Corner. Mahesh has been awarded prestigious Microsoft MVP Award for 7 consecutive years for his contributions to the developer community.";  
  7.   information["Manish"] = "Manish is Very much interested in Microsoft & LifeStyle Accessory Designand working with Microsoft technologies. His expert areas are ASP.NET, ADO.NET, C# .NET and WPF.";  
  8.   var info = prompt("Whose Employee information do you want?""");  
  9.   if ((info == "Dinesh") || (info == "Mahesh") || (info == "Manish")) {  
  10.    var y = document.createElement("y");  
  11.    y.innerText = information[info];  
  12.    document.body.appendChild(y);  
  13.   } else {  
  14.    alert("No Information");  
  15.   }  
  16.  };  
  17.  return Associative_array;  
  18. })();  
  19. window.onload = function() {  
  20.  var greeter = new Associative_array();  
  21.  greeter.EmpInfo();  
  22. };  
Output 1
Enter the employee name for the information and then click on ok
 
 enter-emp-name.jpg
 
Output 2
 
 Result.jpg
 
Output 3
If the specified name is not present in the array then:
 
 enter-wrong-emp-name.jpg
 
 This is message displayed:
 
wrong-msg.jpg 


Similar Articles