“This is the 21st day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge 2021”.
11. Arrays
11.1. The concept of arrays
An array is a collection of data, each of which is called an element. An array can hold any type of element. Arrays are an elegant way to store a set of data under the name of a single variable. Arrays hold groups of related data together and provide easy access.
11.2. Create an array
There are two ways to create arrays in JS. Either way, arrays can hold any type of data, such as strings, numbers, booleans, etc. :
- Create an array using new
- Create an array using an array literal
11.2.1, new Create an array
Var Array name = new Array(); Var arr = new Array(); // Create a new empty arrayCopy the code
11.2.2 Array literals Create arrays
Var Array name = []; / / 2. Use an array literal way to create an array of with the initial value of the var array name = [' white ', 'black', 'rhubarb', 'rich']. Var arrStus = [' white ',12,true,28.9]; var arrStus = [' white ',12,true,28.9];Copy the code
Array literals are square brackets []. Declaring an array and assigning a value to it is called array initialization, and it is the literal method we will use most in the future.
Get the element in the array
We use index (subscript) : the ordinal number used to access the elements of the array (array subscripts start at 0).
An array can be accessed, set, or modified by an index, or retrieved by an array name. If the array is accessed without an element corresponding to the index value, the resulting value is undefined.
Var arrStus = [1,2,3]; // Get the second element in the array alert(arrStus[1]);Copy the code
11.4, Go through the number group
Access each element in the array once from beginning to end (similar to a student roll call) and iterate through each item in the array through the for loop index.
var arr = ['red','green', 'blue'];
for(var i = 0; i < arr.length; i++){
Β Β console.log(arrStus[i]);
}
Copy the code
11.5. Array length
The length of the array is the number of data in the array. The name. Length is used to access the number of elements in the array.
Var arrStus = [1, 2, 3]; alert(arrStus.length); / / 3Copy the code
When we iterate through the array, we find that we always iterate from index 0 to the last index, but if there is a lot of data, it is not easy to get the last index. So here’s an easier way to write it: Use the length of the array to control the number of iterations. If the length of the array is 5, the last index is 4, and we find that the maximum index is always less than the length.
Note:
- The length of the array is the number of elements in the array, not to be confused with the array index.
- When the number of elements in our array changes, the length property changes with it.
- The length property of the array can be modified.
- If the length attribute is greater than the number of elements in the array, a blank element appears at the end of the array.
- If the length attribute is set to a value less than the number of elements in the array, the array elements exceeding that value are removed.
11.6. New elements
New elements can be inserted at the end of an array in the following ways.
Array [array.length] = new data;Copy the code