Unit 8

Arrays with subscripts

Sample 8-1 shows arrays with subscripts.
Notice that weekDays is an object, and has a length property. All arrays are objects, and have a length property. The length was used to print all the values in the array, using the loop:
for (var i = 0; i < weekDays.length; i++)

Literal arrays

sample 8-2 literal arrays shows a literal array and a literal array with one interior item missing.
Do not put nothing after a comma at the end of a literal array; it may produce inconsistent results in different browsers.

Associative arrays

sample 8-3 associative arrays shows different ways to access the data in an associative array. It shows access as an array element. It shows access as a property. It also shows access of all the data, using a for in loop.

Associative arrays have a length zero. Associative arrays toString() method produces an empty string.

Array to String methods

sample 8-4 Array to String methods shows two different methods, used to create a String from an Array.

String to Array method

sample 8-5 the String to Array method is called split(). It converts a string to an array.

Copy an Array

If you just use an assignment with the variable that references an Array, it does not copy the array. It just copies the reference to the Array. So you get two variables, that both refer to the same Array. This is usually not what you want.

You can create a new empty array. Then you can use a loop to copy each entry in the old array into the new array. This works.

sample 8-6 shows another ways to copy an array. Use the Array method toString() to create a string containing a comma separated list of the elements from the array. Then, use the String method split(",") to convert the string into a new array. This works.

If the entries in the array are all primitive elements, this is all you need to do. On the other hand, if the array contains references, you have similar problems copying them.

Copy Arrays methods

sample 8-7 shows methods that copy arrays, in various ways. concat() makes a copy of two arrays, concatenated together. slice() makes a copy of some items in the array; it starts with the first subscript and stops before the ending subscript specified.

Modify Arrays methods

sample 8-8 shows methods that modify arrays, in various ways. reverse() reverses the items in the array, putting the last first, et cetera. The sort() sorts the items in the array. By default, the sort is in ascending string order. You must provide a comparison function, if you want a different comparison used to sort the items.

sample 8-9 shows using splice to remove a sequence of items from an array, inserting a sequence of arrays into an array, and both at the same time.

Queuing Arrays methods

sample 8-10 shows queuing methods that can be used to insert or remove one item from the beginning or end of an Array.

Reading assignment

Reading assignments are in the text book, Java Script, A Beginner's Guide, Second Edition, by John Pollock; McGraw Hill / Osborne, ISBN 0-07-222790-7

Read module 11.
There is additional material in the examples in this web unit, and in the lectures, that is not in module 11. After reading module 11, look at the methods shown in the web examples. Also review this material in the lecture notes after the lecture.

Reading assignments are in the text book, Java Script Concepts & Techniques Programming Interactive Web Sites, by Tina Spain McDuffie; Franklin, Beedle & Associates, ISBN 1-887902-45-7

Read Chapter 7.

Lecture notes

Do NOT read the lecture notes before hearing the lecture. If you do, you will find the lecture very boring. Read the lecture notes if you do not attend the lecture, or if you wish to review the material.