The JavaScript Diaries: Part 12

By Lee Underwood


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Associative Arrays

Another type of array is called an associative array. With this type of array, you can call the element with a string instead of a number. It's easier to use but not useful in a loop because there are no numbers. This type of array can be used for many different things. Let's create one and see what we can do with it.

var musicType = new Array();
  musicType["blues"] = "The Thrill is Gone";
  musicType["rock"] = "Proud Mary";
  musicType["gospel"] = "Amazing Grace";
  musicType["country"] = "Your Cheatin' Heart";

var selectType=prompt("What type of music do you like", "")

if ((selectType=="blues") || (selectType=="rock") || (selectType=="gospel") || (selectType=="country"))
  alert("I think '" + musicType[selectType] + "' is a good song")
  alert("Well, that's nice music too.")
  1. We declared a variable, named it musicType, and initialized it with a value of a new instance of the Array object.
  2. Next, we used text to identify the index instead of numbers, e.g., musicType["blues"] and listed it with its corresponding element (e.g. "The Thrill is Gone").
  3. Then we declared another variable called selectType and initialized it with the value of a prompt statement. We'll use this to gather input from the user.
  4. We then created an if/else statement to process the input from the user. It reads, "If the variable selectType equals blues or rock or gospel or country, then display the alert box on the next line, if not (else) display the alert box on the last line."

Another technique would be to use it in a drop down list. In an external file (or the head section) place the following code:

var offInfo = new Array();
  offInfo["president"] = "Michael Smith: 555-1212";
  offInfo["vpresident"] = "Sherry Gables: 555-4578";
  offInfo["treasurer"] = "Nick Adams: 555-1916";
  offInfo["secretary"] = "Leslie Jones: 555-8745";

function displayOff(offInfo,entry) {
  var nameTele=offInfo[entry];

In the body section:

<form name="musicForm">
<strong>Select Officer:</strong>
<select onChange = "displayOff(offInfo,this.options[selectedIndex].value);">
<option value="">
<option value="president">President
<option value="vpresident">Vice President
<option value="treasurer">Treasurer
<option value="secretary">Secretary
<input class="box" type="text" name="nameTeleBox" value="" readonly size="35">

This one displays a drop down selection box. From that box, a corporate officer can be selected. The officer's name and telephone number will appear below it in the next form field.

You could use CSS to remove the border from the second box to streamline the appearance for your layouts.


In our next installment we wrap up our study of arrays by looking at the different properties and methods associated with them and how to go about manipulating data within them.

This article originally appeared on WebReference.com.

Page 3 of 3

Previous Page
1 2 3

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date