1-array
1.1 The concept of arrays
-
Arrays can store a group of related data together and provide convenient access (acquisition) methods.
-
An array refers to a collection of data , each of which is called an element , and any type of element can be stored in the array . An array is an elegant way to store a set of data under a single variable name.
// 普通变量一次只能存储一个值
var num = 10;
// 数组一次可以存储多个值
var arr = [1,2,3,4,5];
1.2 Create an array
There are two ways to create an array in JS:
-
Use new to create an array
var 数组名 = new Array() ; var arr = new Array(); // 创建一个新的空数组
Note that Array (), A should be capitalized
-
Create an array using array literals
//1. 使用数组字面量方式创建空的数组 var 数组名 = []; //2. 使用数组字面量方式创建带初始值的数组 var 数组名 = ['小白','小黑','大黄','瑞奇'];
-
The literal of the array is square brackets []
-
Declaring arrays and assigning values is called array initialization
-
This literal method is also the method we will use most in the future
-
-
Type of array element
Any type of data can be stored in the array, such as strings, numbers, Boolean values, etc.
var arrStus = ['小白',12,true,28.9];
1.3 Get the elements in the array
Index (subscript): the number used to access the array elements (array subscript starts from 0).
Arrays can access, set, and modify the corresponding array elements by index, and the elements in the array can be obtained in the form of "array name [index]".
// 定义数组
var arrStus = [1,2,3];
// 获取数组中的第2个元素
alert(arrStus[1]);
Note: If the array has no element corresponding to the index value when accessed, the value obtained is undefined
1.4 Traverse the array
-
Array traversal
Each element in the array is visited once from beginning to end (similar to a student's roll call), and each item in the array can be traversed through the for loop index
var arr = ['red','green', 'blue'];
for(var i = 0; i < arr.length; i++){
console.log(arrStus[i]);
}
2. The length of the array
The length of the array: By default, the number of elements in the array
Use "array name.length" to access the number of array elements (array length).
var arrStus = [1,2,3];
alert(arrStus.length); // 3
Note :
-
The length of the array here is the number of array elements, not to be confused with the index number of the array.
-
When the number of elements in our array changes, the length property changes along with it
-
The length property of the array can be modified:
-
If the set length attribute value is greater than the number of elements in the array, a blank element will appear at the end of the array;
-
If the set length attribute value is less than the number of elements in the array, the array elements exceeding this value will be deleted
3. Calculate the sum and average of the array
var arr = [1, 5, 7, 2, 3];
var sum = 0;
var average = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
average = sum / arr.length;
console.log(sum);
console.log(average);
4. Find the maximum and minimum of the array
var arr = [3, 85, 22, 33, 16];
var max = arr[0];
for (var i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
console.log("该数组最大值为:" + max);
for (var i = 0; i < arr.length; i++) {
if (arr[i] < max) {
max = arr[i];
}
}
console.log("该数组最小值为:" + max);
1.5 New elements in the array
You can insert new elements at the end of the array in the following ways:
数组[ 数组.length ] = 新数据;