Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Last time we looked at JavaScript string objects, this time we’ll look at array objects, which are a relatively important collection of data. The elements of an array object allow different types of data.
Array Array object
Array Array object is an important knowledge in JavaScript, which is used most in work.
An array is an ordered collection of data whose elements are allowed to contain data of different types. You can get elements using array names and subscripts
Creation of an array object
In the actual project development process, it is often necessary to deal with data with the same attributes and location related, such as various item list displays/membership list information, etc.
An array is a combination of variables of the same data type. The variables in the array are written as elements of an array that can be retrieved/accessed/modified by index. But array subscripts (‘ indexes ‘) start at 0. The number of elements an array can hold is called the length of the array, which has the same length property as a string.
Array object creation method:
- The most direct, direct statement
The syntax format is
[var] array variable = [] note:`var`Yes Optional.Copy the code
Create an array object myArray and assign it a value as follows:
var myArray = [1.2.3.5.9.10.13.21]
Copy the code
- Create an array object of length 0
new
The keyword
Create an array object using the new keyword as follows:
[var] array object =new Array(String)Copy the code
Note: The var keyword is optional, and the first letter of the string constructor Array() must be uppercase. For example, create a string object myArray with the new keyword and assign it a value as follows:
var myArray = new Array([1.2.3.5.9.10.13.21])
Copy the code
- Create an array object of length N
new
The keyword
var myArray = new Array(n)
Copy the code
Of course, the keyword new is usually used to create an array is relatively rare, see the source code will use direct declaration method is much easier, right!
- Create a new array of the specified length and assign it
var myArray = new Array(1.2.3.5.6.8.9.13.21)
// [1, 2, 3, 5, 6, 8, 9, 13, 21]
Copy the code
Accessing an array element:
An array element is accessed by the array’s sequence number. In JavaScript, array element sequence number I starts at 0, that is, the subscript I of the first element is 0, followed by 1
Array elements can be evaluated or assigned in this manner with the syntax:
Array variable name = value variable name = array variable name [I]Copy the code
If we want to read or assign to the elements of an array object, we can use the for statement or for… In statement. The format of the in statement is as follows:
for(varThe variable nameinAn array name) {// The body of the for loop
// Operate on arrays
// The variable is named for each item in the array
}
Copy the code
The for statement uses the length property of the array to traverse the array.