140 likes | 376 Views
Arrays. What are arrays? Examples Defining an array Using an array Notes about arrays Records MiniLab #4. What Are Arrays?. Lists of similar things. Way to group items for easy access. Definition - an indexed collection of objects of any type. Examples. List of numbers List of names
E N D
Arrays • What are arrays? • Examples • Defining an array • Using an array • Notes about arrays • Records • MiniLab #4
What Are Arrays? • Lists of similar things. • Way to group items for easy access. • Definition - an indexed collection of objects of any type.
Examples • List of numbers • List of names • List of student records • List of candy available • List of CD information • List of classes to take • … many more!
Defining an Array in JavaScript • JavaScript has built-in help with arrays. • Easiest way to define an array is with a function. • this refers to the array before it’s been given a name.
Defining an Array - Example function makeColorArray() {this.length = 7;this[0] = "red";this[1] = "orange";this[2] = "yellow";this[3] = "green";this[4] = "blue";this[5] = "indigo";this[6] = "violet"; }
Using an Array • The array isn't really created until you call the function. • var colors = new makeColorArray(); • Now colors is an array of color names.
Notes About Arrays • You can refer to the length of an array by using name.Length. For example, colors.lengthrefers to the length of the color array which is 7. • You may use colors.length anywhere you would use the number of items in the array. That way if you change the number of items in the array, it’s updated for you automatically everywhere in your program.
Notes About Arrays • If the number of elements in the array is n, then the elements are numbered 0 through n - 1 • To access an item in an array use the brackets around a number. • For example, colors[0]is the first element in the array. And colors[colors.length-1]is the last element in the array.
Arrays of Records • Records are used to group several, possibly dissimilar, pieces of information into a single unit. • You create records using the new command and a function. • Remember that all functions reside in between the <HEAD> and </HEAD> tags in your web page.
Arrays of Records - Example Create an array to contain both the name and phone number of each student in our class. function makeStudentRecord (studentName, studentPhone) { this.name = studentName; this.phone = studentPhone; }
Arrays of Records - Example function makeStudentArray () { this.length = 2; this[0] = new makeStudentRecord (“Kelly”, “337-5721”); this[1] = new makeStudentRecord (“Pam”, “337-7064”); }
Using a Record • Somewhere in our web page we must still create the array. • var StudentArray = new makeStudentArray(); • Now StudentArray is an array of records that contain a student name and a student phone number.
Notes About Records • You must still access the items in the array with brackets but now there is an added way to get to the record parts. • For example, StudentArray[0]is the first element in the array but StudentArray[0].name is the name part of the record. • And StudentArray[0].phoneis the phone part of the first element.
MiniLab #4 • You will be creating an array of records of your own. The items in the array can be the same as in the lab or you can be creative and make up your own items. • document.writeln(“<strong>Name: “ + studentArray[0].name + “</strong>”); will print the name of the first student in your array onto your web page in bold.