Organize JavaScript arrays learn notes _javascript tips

Source: Internet
Author: User
Tags array definition array length javascript array

1, what is an array
An array is a collection of values, each with an index number, starting with 0, each index has a corresponding value, and additional values are added as needed.

 <script type= "Text/javascript" >
  var myarr=new array ();//define array
  myarr[0]=80; 
  myarr[1]=60;
  myarr[2]=99;
  document.write ("The first person's achievement is:" +myarr[0]);
  document.write ("The second person's score is:" +myarr[1]);
  document.write ("The third person's score is:" +myarr[2]);
 </script>

2, group, and give the regiment a name (how to create an array)
You first create the array before you use it, and you need to assign the array itself to a variable.
To create an array syntax:

The Var myarray=new array ();//statement is the variable new array () that creates a new array stored in the myarray variable
var myarray holds the array
; Creates a new empty array

When we create an array, we can also specify the length of the arrays, and the length can be arbitrarily specified.

Copy Code code as follows:
var myarray= new Array (8); Creates an array that stores 8 of data.

Note:
1. The new array created is an empty array with no values, such as output, that displays undefined.
2. Although the length is specified when an array is created, the array is actually longer, which means that the element can still be stored outside the specified length even if the length is 8.

3. Array Assignment
The first step: Group A bus
The second step: according to the vote
Bus No. 1th seat is John.
Bus No. 2nd seat is Dick.
Array expression:
The first step: Create an array of var myarr=new array ();
Step Two: Assign values to arrays
Myarr[1]= "John";
Myarr[2]= "Dick";
Create an array to store math scores for 5 people:

var myarray=new Array (); Create a new empty array
myarray[0]=66//store 1th person's score
myarray[1]=80;//Store 2nd person's results
myarray[2]=90;//store 3rd person's results
myarray[3]=77; Store 4th person's results
myarray[4]=59//Storage 5th person's score

Note: each value in the array has an index number, starting at 0.
The first method:

Copy Code code as follows:
var myarray = new Array (66,80,90,77,59);//Create an array and you can assign values

The second method:
Copy Code code as follows:
var myarray = new array[66,80,90,77,59];//Enter an array directly (called "literal array")

4. Add a new element to the array
With just the next unused index, you can add new elements to the array at any time.
myarray[5]=88; Adds a new element to an array using a new index

5. Using array elements
To get the value of an array element, simply reference the array variable and provide an index, such as:
The first person's achievement expresses method: Myarray[0]
The third person's achievement expresses method: Myarray[2]

<script language= "JavaScript" >
 var myarr=new Array ();
  myarr[0]= "Small red";
  myarr[1]= "Xiao Ming";
  myarr[2]= "small light";
  Myarr[3]= "Ogawa";
  document.write ("Second person's name is:" + myarr[1]);
</script>

6, understand the number of members (array property length)
The Length property represents the size of the array, that is, the number of elements in the array.

Copy Code code as follows:
Myarray.length; To get the length of an array myarray

Note: Because 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 the array is 5, and the upper and lower bounds of the arrays are 0 and 4 respectively.

 The Var arr=[55,32,5,90,60,98,76,54];//contains 8 numeric arrays arr 
 document.write (arr.length);//display array length 8
 document.write (arr [7]); Displays the value of the 8th element 54

Also, the length property of a JavaScript array is variable, which requires special attention.

arr.length=10; Increases the length of the array
document.write (arr.length);//array length has changed to 10

The length of the array changes as the element increases, as follows:

var arr=[98,76,54,56,76]; Array
document.write (arr.length) with 5 numeric values;///display array length 5
arr[15]=34;//add element, use index 15, assign value to
alert ( Arr.length); Displays the length of the array 16

7, two-dimensional array
One dimensional array, we look at a group of boxes, each box can only put one content.
Representation of one-dimensional arrays: myarray[]
Two-dimensional arrays, we look at a group of boxes, but there are multiple boxes in each box.
Representation of two-dimensional arrays: myarray[[]
Note: The index value of the two dimensions of the two-dimensional array also starts at 0, and the last index value of the two dimensions is length-1.
1. A method for defining a two-D array

var myarr=new Array (); Declare one-dimensional 
for (Var i=0;i<2;i++) {//One dimension length 2
  myarr[i]=new Array ();//Then declare two-dimensional for 
  (var j=0;j<3;j++) {// The two-dimensional length is 3
   myarr[i][j]=i+j//assignment, and the value of each array element is I+j
  }
 }

2. Two-D array definition method two

Copy Code code as follows:
var myarr = [[0, 1, 2],[1, 2, 3,]]

3). Assign Value
Copy Code code as follows:
myarr[0][1]=5; The value of 5 is passed into the array, overwriting the original value.

Description: Myarr[0][1], 0 represents the rows of the table, and 1 represents the columns of the table.

That's all about the JavaScript array, and it's a further study of JavaScript arrays, and I hope you like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.