1, Overview of arrays
The concept of array (standard definition): an array is a container that can store a set or a series of related data.
The concept of array (Understanding definition): a set of numbers, called "array".
For example, find the average of the sum of the ages of all students? var age1 = 20; var age2 = 23; var age3 = 26; . . . var age60 = 21; var sum = age1 + age2 + age3 +....+ age60; var n=60; var arr = [10,20,30,40,50];
This is very troublesome and has a large workload. In case the number of people increases or decreases, it is very inconvenient. It's convenient to loop with arrays.
var arr = ["Zhang San", "male", 24, "undergraduate", "Peking University];
Array element: refers to the data segment stored in the array and given a unique index number. Simply put, each value in the array is called an "array element".
Function of array: to solve the problem of storage and use of a large number of related data.
E.g. scattered households in villages and buildings can be found by house number.
2, Array creation
(1) Use the new keyword and Array() to create an array (also known as a constructor method)
var arr = new Array();//Create an empty array var arr = new Array(""Zhang San",""Male",24,""Unmarried",""Undergraduate",""Peking University");//Create an array and initialize the elements of the array.
(2) Create an array using []
var arr = [""Zhang San",""Male",24,""Unmarried",""Undergraduate",""Peking University"];
3, Array common knowledge
1. Index of array
a. There are multiple values in the array, and each value has a "number". Each value in the array can be accessed through "number". The "number" in the array is also called "subscript" or "index mark".
b. The lower number in the array is a positive integer starting from 0. That is, the subscript of the first array element is 0, and the subscript of the second array element is 1,
The third array element subscript is 2, and so on.
c. The subscript of the first array element must be 0, and the subscript of the last array element is: length - 1;
d. The purpose of using arrays is to use loops to traverse arrays, which is very convenient.
2. Array properties
Array object attribute length: is the total number of elements in the index group.
3. Access to array elements
Access to array elements, for example: var arr=[10,20,30,40,50]; var arr=["Zhang San","male",24,"undergraduate","Peking University"]; The access method is: array variable name, followed by a square bracket[],[]Inside square brackets are the subscripts of array elements. For example: arr[3];
4. Array operation
1)Read elements: read elements with existing subscripts. For example: var age = arr[2]; 2)Modify element: modify the value of the element with existing subscript (reassign). For example: arr[2] = 25; 3)Add element: add an element with a nonexistent subscript. 4)Delete element: use keyword delete,Only the element value can be deleted, and the subscript is still there. Example: use an array to save personal information and output results.
5. Traversal of array
What is traversal? Is to read or write data one by one from the array.
1) Normal for loop
var arr6 = [1, 2, 3, 4, 5, 6]; alert(arr6); Sequential traversal for(var i = 0; i<arr.length; i++) { alert(arr6[i]); } Reverse order output for(var i = arr.length-1; i>=0; i--) { alert(arr[i]); }
2) The for... in statement is used to traverse the attributes of arrays or objects (fast traversal). Function: only arrays and objects can be traversed
Syntax: for (variable in set) set: array name and object name
for(var i in arr) { alert(arr[i]); }
6. Array method
shift(): Delete the first element in the array and subtract the length by 1. pop():Delete the last element in the array and subtract the length by 1. unshift():Add one or more array elements to the front of the array, and the length should be changed. push():Add one or more array elements to the end of the array, and the length should be changed. concat() : Method is used to join two or more arrays, Do not change the original array. Returns a new array. reverse() Reverse sort, The original array is also sorted in reverse toString() Convert array to string join("Splice)Convert the array into a string, and connect it with a splice in the middle slice(start,end): Do not modify the original array, Extract the specified area data from the original array. start start end end. If the parameter is negative, it starts from the reciprocal. End position not included splice Delete: used to delete elements. There are two parameters: the first parameter (the position of the first item to be deleted) and the second parameter (the number of items to be deleted) Insert: inserts any item element into the array at the specified position. Three parameters, first parameter (start position), second parameter (0), third parameter (inserted item) Note: if the second parameter (0) is 0, no items will be deleted Replace: insert any item element into the specified position of the array, and delete any number of items at the same time, including three parameters. The first parameter (starting position), the second parameter (number of items deleted), and the third parameter (insert any number of items) sort() Method to sort the array a-b If its value is positive, swap their positions b-a If the values are positive, swap their positions