Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

The first number is the subscript of the first dimension, whereas the second one specifies the subscript of the second dimension.

Creation with a Single Constructor Function

The easiest way to create a two-dimensional array (2DA) is to use a constructor function. Here it is:

function Array2D(dim1, dim2) {
  for (var i = 0; i < dim1; ++i) {
 this[i] = new Array(dim2)
  }
  this.length = new Array(dim1, dim2)
}

The following script demonstrates the creation and usage of an array created by this function:

var ar = new Array2D(4,7)
ar[2][1] = 6
ar[0][0] = "Hello" // this is the "first" element
ar[3][6] = true // this is the "last" element
alert("Length of first dimension is " + ar.length[0])
alert("Length of second dimension is " + ar.length[1])
alert("\"Last\" element: " + ar[ar.length[0] – 1][ar.length[1] – 1])

The messages displayed via alert boxes are:

Length of first dimension is 4
Length of second dimension is 7
"Last" element: true

The constructor function uses a loop. The first array (ar) is not an explicit Array object. It is simply a regular object whose properties are specified using the array notation. The built-in methods of the Array object type do not operate on this array. The loop executes dim1 times, the length of the first dimension. Each property (or element) of the calling object is assigned a “real” array consisting of dim2 elements. Since these are real arrays, you can use the built-in methods and properties of the Array object type. For example, ar[0] is an array of dim2 elements, so you can refer to its elements: ar[0][0], ar[0][1], ar[0][2], etc. The 2DA is actually a one-dimensional array of which all elements are one-dimensional arrays. Therefore, you should be careful not to ruin the structure of the 2DA by assigning values to its main array, such as:

ar[3] = "Do not do this to a 2DA!"

The preceding constructor method also includes a length property which is an array of two elements. The first element holds the number of elements in the main array (the specified length of the first dimension) while the second element holds the specified length of the second dimension. The example demonstrates this best. Keep in mind that these are static properties. Assigning a value to an element of the length property does not affect the array, but can trip you up when you need the correct values regarding the length of the array’s dimensions.

Creation without Constructor Function

It is possible to create a two-dimensional array without a constructor function. This method is based on creating instances of the Array object. Here it is:

function addDim2(array1D, dim2) {
  for (var i = 0; i < array1D.length; ++i) {
 array1D[i] = new Array(dim2)
  }
  return array1D
}

This function alone means almost nothing, so take a look at a working example:

var dim1 = 4
var dim2 = 7
var ar = new Array(dim1)
ar = addDim2(ar, dim2)
ar[2][1] = 6
ar[0][0] = "Hello" // this is the "first" element
ar[3][6] = true // this is the "last" element
alert("Length of first dimension is " + ar.length)
alert("Length of second dimension is " + ar[0].length)
alert("\"Last\" element: " + ar[ar.length – 1][ar[0].length – 1])

At first, the length of the first dimension, 4, is assigned to dim1. The length of the second dimension is then assigned to dim2. A new array, named ar, is created according to the Array object type. Its length is the length of the first dimension of the desired array. The function addDim2 is then called with the array and the length of the second dimension. It returns the final 2DA to the original one-dimensional array, ar. The function is based on a simple loop that assigns an array of dim2 elements to each element of the original one-dimensional array. Since both the first dimension array and the second dimension array are instances of the Array object type, you can refer to the length of each dimension in the following way:

ar.length == length of first dimension
ar[0].length == length of second dimension == ar[1].length == ...

Note that for the second expression to be true, you must not change the length of the 2DA at any time during the script execution. Otherwise, it does not simulate a 2DA anymore.

Associative Arrays

Associative arrays use strings as subscripts. For example, an element of the array can be:

color["yellow"] = "FFFF00"

Such arrays group together related data. These arrays are actually objects, except that you use square brackets instead of a dot. Another important difference is that array specification (square brackets) enables you to refer to the value of a variable as the property of method specification rather than the actual literal. For example:

var col = "yellow"
color[col] = "FFFF00"

Here is another interesting example:

var w = "write"
document[w]("Hello!") // prints "Hello!"

If you replace the array notation with the regular “dot” specification you receive an error:

var w = "write"
document.w("Hello!")

The “dot” notation requires the actual literal specified, as it does not evaluate the written value. You can use associative arrays to create nested objects, resembling multidimensional arrays. For example, you can create an associative array where its subscripts are names of students in a class, so each element of the associative array is an object containing fields such as the student’s grade, age, and so on.

Note that associative arrays cannot be created as instances of the built-in Array object. You must use a constructor function to create one. Here is the example with the students:

function studentClass() {
  for (var i = 0; i < arguments.length; ++i) {
 this[studentClass.arguments[i]] = new student()
  }
}

function student() {
  // this.grade = null
  // this.age = null
}
var students = new studentClass("Yehuda", "Yael",
 "Dikla", "Tomer", "Carmit", "Alon")
students["Yehuda"].grade = 40
students["Alon"].age = 11
students["Dikla"].grade = "N/A"
alert(students["Dikla"].grade)

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us