Home >Web Front-end >JS Tutorial >Organize Javascript array study notes_javascript skills

Organize Javascript array study notes_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:28:591263browse

1. What is an array
An array is a collection of values, each value has an index number, starting from 0, and each index has a corresponding value, adding more values ​​as needed.

 <script type="text/javascript">
  var myarr=new Array(); //定义数组
  myarr[0]=80; 
  myarr[1]=60;
  myarr[2]=99;
  document.write("第一个人的成绩是:"+myarr[0]);
  document.write("第二个人的成绩是:"+myarr[1]);
  document.write("第三个人的成绩是:"+myarr[2]);
 </script>

2. Form a group and give it a name (how to create an array)
Before using an array, you must first create it, and assign the array itself to a variable.
Create array syntax:

var myarray=new Array();//语句是创建一个新数组存储在myarray变量中
var myarray保存数组的变量
new Array();创建一个新的空数组

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

Copy code The code is as follows:
var myarray= new Array(8); //Create an array and store 8 pieces of data .

Note:
1). The new array created is an empty array with no value. If output, it will display undefined.
2). Although the length is specified when creating an array, arrays are actually variable-length, which means that even if the length is specified to be 8, elements can still be stored beyond the specified length.

3. Array assignment
Step one: form a bus
Step 2: Take your seat according to your ticket number
Seat No. 1 on the bus is Zhang San
Seat No. 2 on the bus is Li Si
Array expression:
Step 1: Create an array var myarr=new Array();
Step 2: Assign a value to the array
myarr[1]="Zhang San";
myarr[2]="李思";
Create an array to store the math scores of 5 people:

var myarray=new Array(); //创建一个新的空数组
myarray[0]=66; //存储第1个人的成绩
myarray[1]=80; //存储第2个人的成绩
myarray[2]=90; //存储第3个人的成绩
myarray[3]=77; //存储第4个人的成绩
myarray[4]=59; //存储第5个人的成绩

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

Copy code The code is as follows:
var myarray = new Array(66,80,90,77,59);/ / Create an array and assign values ​​at the same time

Second method:
Copy code The code is as follows:
var myarray = new Array[66,80,90,77,59];/ /Directly input an array (called "literal array")

4. Add a new element to the array
New elements can be added to the array at any time by simply using the next unused index.
myarray[5]=88; //Use a new index to add a new element to the array

5. Use array elements
To get the value of an array element, just reference the array variable and provide an index, like:
The first person’s score expression method: myarray[0]
The third person’s score expression method: myarray[2]

<script language="javascript">
 var myarr=new Array();
  myarr[0]="小红";
  myarr[1]="小明";
  myarr[2]="小亮";
  myarr[3]="小川";
  document.write("第二人的姓名是:"+ myarr[1] );
</script>

6. Understand the number of members (array attribute length)
The Length attribute represents the length of the array, that is, the number of elements in the array.

Copy code The code is as follows:
myarray.length; //Get the length of the array myarray

Note: Because the index of an array always starts from 0, the upper and lower limits of an array are: 0 and length-1 respectively. For example, if the length of the array is 5, the upper and lower limits of the array are 0 and 4 respectively.
 var arr=[55,32,5,90,60,98,76,54];//包含8个数值的数组arr 
 document.write(arr.length); //显示数组长度8
 document.write(arr[7]); //显示第8个元素的值54

At the same time, the length property of JavaScript arrays is variable, which requires special attention.

arr.length=10; //增大数组的长度
document.write(arr.length); //数组长度已经变为10

As the number of elements increases, the length of the array will also change, as follows:

var arr=[98,76,54,56,76]; // 包含5个数值的数组
document.write(arr.length); //显示数组的长度5
arr[15]=34; //增加元素,使用索引为15,赋值为34
alert(arr.length); //显示数组的长度16

7. Two-dimensional array
We think of a one-dimensional array as a set of boxes, each box can only hold one content.
Representation of one-dimensional array: myarray[ ]
We think of a two-dimensional array as a set of boxes, but each box can also contain multiple boxes.
Representation of two-dimensional array: myarray[ ][ ]
Note: The index values ​​of the two dimensions of the two-dimensional array also start from 0, and the last index value of the two dimensions is length-1.
1). Method 1 of defining two-dimensional array

var myarr=new Array(); //先声明一维 
for(var i=0;i<2;i++){ //一维长度为2
  myarr[i]=new Array(); //再声明二维 
  for(var j=0;j<3;j++){ //二维长度为3
   myarr[i][j]=i+j; // 赋值,每个数组元素的值为i+j
  }
 }

2). Two-dimensional array definition method 2

Copy code The code is as follows:
var Myarr = [[0 , 1 , 2 ],[1 , 2 , 3 , ]]

3). Assignment
Copy code The code is as follows:
myarr[0][1]=5; //Pass the value of 5 in into the array, overwriting the original value.

Explanation: myarr[0][1], 0 represents the row of the table, and 1 represents the column of the table.

The above is all about Javascript arrays. It is a further study of Javascript arrays. I hope you like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn