Group and take a name for the regiment (how to create an array)
The array is created first, and the array itself needs to be assigned to a variable. Like we travel, to group, and to set a name "Yunnan tour."
To create an array syntax:
var myarray=new Array ();
As we create arrays, we can also specify the length of the array, which can be arbitrarily specified.
Attention:
1. The new array created is an empty array with no value, such as output, to display undefined.
2. Although a length is specified when an array is created, the array is actually long, meaning that the element can still be stored outside the specified length even if the length is specified as 8.
1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />5 <title>Create an array</title>6 <Scripttype= "Text/javascript">7 varMyarr=NewArray ();8 document.write ("the first value of the array:"+myarr[0]);9 </Script>Ten </Head> One <Body> A </Body> - </HTML>
Understanding the number of members (array property length)
If we want to know the size of the array, simply reference an attribute of the array length. The Length property represents the size of the array, which is the number of elements in the array.
Grammar:
Myarray.length; Get the length of the array myarray
Note: since the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1, respectively. The length of an array is 5, and the upper and lower bounds of the arrays are 0 and 4, respectively.
var arr=[55,32,5,90,60,98,76,54];//contains an array of 8 values arr document.write (arr.length); Display array length 8document.write (arr[7]); Displays the value of the 8th element 54
At the same time, the length property of the JavaScript array is variable, which requires special attention.
arr.length=10; Increase the length of the array document.write (arr.length); Array length has changed to 10
The length of the array changes as the element grows, as follows:
var arr=[98,76,54,56,76]; An array containing 5 values document.write (arr.length); Displays the length of the array 5arr[15]=34; add element, use Index to 15, assign value to 34alert (arr.length); Displays the length of the array 16
1 <!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />5 <title>Array length</title>6 <Scriptlanguage= "JavaScript">7 varMynum=NewArray ( $, -, the,98);8 document.write ("the length of the array is:"+ (mynum.length) );9 </Script>Ten </Head> One <Body> A </Body> - </HTML>
two-dimensional arrays
One-dimensional arrays, we look at a set of boxes, each box can only be one content.
Representation of one-dimensional arrays: myarray[]
Two-dimensional arrays, we look at a set of boxes, but each box can also put more than one box.
Representation of a two-dimensional array: myarray[[]
Note: the index values for the two dimensions of a two-dimensional array are also starting from 0, and the last index value for two dimensions is length-1.
1. How to define a two-D array
var myarr=new Array (); First declare one-dimensional for (Var i=0;i<2;i++) { //one-dimensional length of 2 myarr[i]=new Array (); Re-declare the two-dimensional for (Var j=0;j<3;j++) { //two-D length is 3 myarr[i][j]=i+j; Assignment, the value of each array element is i+j }}
Note: for the FOR Loop statement, see Chapter Fourth 4-5.
The above two-dimensional array is represented in a tabular way:
2. methods for defining two-dimensional arrays two
var myarr = [[0, 1, 2],[1, 2, 3,]]
3. Assigning values
myarr[0][1]=5; The value of 5 is passed into the array, overwriting the original value.
Description: myarr[0][1], 0 represents the table row, and 1 represents the table column.
JavaScript Step-by-step article