User Guide

JavaScript syntax arrays 45
// JavaScript syntax
var list1 = list(5,10);
var list2 = list(15,20);
var mdList = list(list1, list2);
trace(mdList[1][2]); // displays 10
trace(mdList[2][1]); // displays 15
JavaScript syntax arrays
JavaScript syntax arrays are similar to Lingo-style linear lists in that each element in an array is a
single value. One of the main differences between JavaScript syntax arrays and Lingo-style linear
lists is that the index into an array always starts with 0.
You create a JavaScript syntax array by using the Array object. You can use either square brackets
(
[ ]) or the Array constructor to create an array. The following two statements create an array
with two values.
// JavaScript syntax
var myArray = [10, 15]; // using square brackets
var myArray = new Array(10, 15); // using the Array constructor
You can also create empty arrays. The following two statements create an empty array.
// JavaScript syntax
var myArray = [];
var myArray = new Array();
Note: The Director Scripting Reference does not include a complete reference for JavaScript syntax
Array objects. For more complete information on using Array objects, see one of the many third-party
resources on the subject.
Checking items in arrays
You can determine the characteristics of an array and the number of items the array contains by
using the following methods.
To display the contents of a list, use the put() or trace() function, passing the variable that
contains the list as a parameter.
To determine the number of items in an array, use the Array object’s length property.
To determine an arrays type, use the constructor property.
The following example illustrates determining the number of items in an array using the
length property, and then returning the type of object using the constructor property.
// JavaScript syntax
var x = ["1", "2", "3"];
trace(x.length) // displays 3
trace(x.contructor == Array) // displays true
Adding and deleting items in arrays
You can add or delete items in an array by using the following methods:
To add an item at the end of an array, use the Array objects push() method.
To add an item at its proper position in a sorted array, use the Array object’s splice() method.
To add an item at a specific position in an array, use the Array object’s splice() method.