var student=new Array(); student["Name"]="John Doe"; //one key, one value student["Courses"]=new Array("Math","English", "PE"); student["Phones"]=new Array("415-333-1234","530-345-5432"); document.write("The student's name is " + student["Name"] + "."); document.write("His courses are " + student["Courses"] + "."); document.write("His favorite course is "+ student["Courses"][2] + "."); document.write("His cell phone number is " + student["Phones"][0] + ".");
EXPLANATION A new array object called student is instantiated An index or key in the student array is a string of text, “Name” assigned a single value, “John Doe” Now there is an association between the index and the value The index key is “Courses” The student takes more than one course A new Array is created with the values of the courses assigned to the key, an array with a nested array The index key is “Phones” The student has two phones A new Array is created with two phone numbers assigned to the key, an array with a nested array To get the name of the student, the “Name” is used as an index, also called the key To get a list of all the courses, the key/index in the array is “Courses” To get one of the courses out of the list, two index values are needed, one to key into the courses and one to select a specific course Because array index values start at 0, student [“Courses”][2] gets the third course, “PE” Again two index values are need to get one of the phone numbers student["Phones"][0] gets the value of the first phone The output is shown in Figure 9.12 From the Library of WoweBook.Com 9.3 Array Methods Figure 9.12 9.3 227 An associative array—one key associated with more than one value Array Methods Because an array is an object in JavaScript, it has properties to describe it and methods to manipulate it The length property of an array was used in previous examples to determine the size of an array Now we will look at methods that allow you to manipulate arrays such as adding a new element at the beginning or end of an array, removing an element from the end of an array, reversing an array, and so on JavaScript provides a whole set of methods for doing all of these things and more (see Table 9.2) Table 9.2 Array Methods Method What It Does concat() Concatenates elements from one array to another array join() Joins the elements of an array by a separator to form a string pop() Removes and returns the last element of an array push() Adds elements to the end of an array reverse() Reverses the order of the elements in an array shift() Removes and returns the first element of an array slice() Creates a new array from elements of an existing array sort() Sorts an array alphabetically or numerically splice() Removes and/or replaces elements of an array toLocaleString() Returns a string representation of the array in local format toString() Returns a string representation of the array unshift() Adds elements to the beginning of an array From the Library of WoweBook.Com ... type="text /javascript" > for(var i in book){ document.write("book[" + i + "] "+ book[i] + ""); } From the Library of WoweBook.Com 216 Chapter • JavaScript. .. Pets var pet = [ "Fido", "Slinky", "Tweetie","Wanda" ]; for(var i in... WoweBook.Com 218 Chapter EXAMPLE • JavaScript Core Objects 9.3 The Array Object An Array of Numbers var years = new