Creating and accessing an array object
Associative Array
This is an example of a Javascript Array Object which is similar in structure to a database table. The record array itself is the table and within that array are the field arrays which represent an individual record listing each field or property of that record. For each new record you must call the object constructor and increment the element number.
Records are accessed using the syntax arrayname[Element number].property
record[1] = new field(); // Create a second object to the array.
Associative array exampleThe code for this example is listed below
<SCRIPT LANGUAGE=JAVASCRIPT TYPE ="TEXT/JAVASCRIPT">
<!--
//declare 'record' as a global array variable.
var record = new Array();
record[0] = new field("Peter","Centreville","VA");// function call to object constructor.
function associative()
{
alert("The first name is " + record[0].name +" and the listed city is " +record[0].city);
record[0].name = "Peter Lorre"; // Update name from 'Peter' to 'Peter Lorre'
alert("The name field of the First element has been changed to " + record[0].name);
record[1] = new field(); // Create a second object to the array.
record[1].name = "Charles"; //Assign values to array object.
record[1].city = "Springfield";
record[1].state = "NC";
record[1].address = "1313 Elm Street";// create field that is not part of the original object.
record[2] = new field("Alan","Allentown","PA"); // Add new record to array.
alert("address: " + record[1].address);
alert( record[1].city + " state: " + record[1].state);
}
function field(name,city,state)
{
//object constructor. Order of arguments passed to the function must be the same as the function calls arguments.
this.name= name;
this.city = city;
this.state = state;
}
-->
</SCRIPT>