|
February 29, 2000 Associative Arrays Tips: February 2000
Yehuda Shiran, Ph.D.
|
|
Associative arrays use strings as
subscripts. For example, an element of the array can be:
Here is another interesting example:
If you replace the array notation with the regular "dot" specification you receive an error:
The "dot" notation requires the actual literal specified, as it does not evaluate the written value. You can use associative arrays 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 containg 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:
Learn more about associative array usage in JavaScript in Column 58, The Doc Dialer, Part II.
People who read this tip also read these tips: Look for similar tips by subject:
|