Web design templates - 77Chapter 7Programming Fundamentals, Part IIitem in an array,
77Chapter 7Programming Fundamentals, Part IIitem in an array, you need to know the name of the array and the index for the row. Becauseindex values start with zero, the total number of items of the array (as determined by thearray s lengthproperty) is always one more than the highest index value of the array. More advanced array concepts enable you to create the equivalent of an array with multiple columns (described in Chapter 30). For this tutorial, I stay with the single- column basic array. Data elements inside JavaScript arrays can be any data type, including objects. And, unlike alot of other programming languages, different rows of the same JavaScript array can containdifferent data types. Creating an arrayAn array is stored in a variable, so when you create an array you assign the new array objectto the variable. (Yes, arrays are objects, but they belong to the core JavaScript languagerather than the document object model.) A special keyword new preceding a call to theJavaScript function that generates arrays creates space in memory for the array. An optionalparameter to the Array()function enables you to specify at the time of creation how manyelements (rows) of data eventually will occupy the array. JavaScript is very forgiving aboutthis because you can change the size of an array at any time. Therefore, if you omit a parame- ter when generating a new array, your script incurs no penalty. To demonstrate the array creation process, I create an array that holds the names of the50states plus the District of Columbia (a total of 51). The first task is to create that arrayandassign it to a variable of any name that helps me remember what this collection of dataisabout: var USStates = new Array(51); At this point, the USStatesarray is sitting in memory like a 51-row table with no data in it. Tofill the rows, I must assign data to each row. Addressing each row of an array requires a spe- cial way of indicating the index value of the row: square brackets after the name of the array. The first row of the USStatesarray is addressed asUSStates[0] To assign the string name of the first state of the alphabet to that row, I use a simple assign- ment operator: USStates[0] = Alabama ; To fill in the rest of the rows, I include a statement for each row: USStates[1] = Alaska ; USStates[2] = Arizona ; USStates[3] = Arkansas ; … USStates[50] = Wyoming ; Therefore, if you want to include a table of information in a document from which a scriptcanlook up information without accessing the server, you include the data in the documentin the form of an array creation sequence. When the statements run as the document loads, by the time the document finishes loading into the browser, the data collection array is builtand ready to go. Despite what appears to be the potential for a lot of statements in a docu- ment for such a data collection, the amount of data that must download for typical array
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services