An array object uses a separate variable name to store a series of values.
Instance
-
Create an array
-
Create an array, assign values to it, and then output these values.
-
For... in statement
-
Use the for... in Declaration to cyclically output the elements in the array.
-
Merge two Arrays-Concat ()
-
How to Use the Concat () method to merge two arrays.
-
Using array elements to form a string-join ()
-
How to Use the join () method to make all elements of an array a string.
-
Text array-sort ()
-
How to Use the sort () method to sort arrays literally.
-
Number array-sort ()
-
How to Use the sort () method to sort arrays from numeric values.
Complete array Object Reference Manual
We provide Javascript array Object Reference Manual, which includes all attributes and methods that can be used for array objects.
This Manual contains a detailed description of each attribute and method and related examples.
Define an array
An array object is used to store a series of values in a separate variable name.
We use the keyword new to create an array object. The followingCodeDefines an array object named myarray:
VaR myarray = new array ()
There are two ways to assign values to an array (you can add any number of values, just as you can define any number of variables you need ).
1:
VaR mycars =New array ()
Mycars [0] = "Saab" mycars [1] = "Volvo" mycars [2] = "BMW"
You can also use an integer independent variable to control the array capacity:
VaR mycars =New array (3)
Mycars [0] = "Saab" mycars [1] = "Volvo" mycars [2] = "BMW"
2:
VaR mycars =New array ("Saab", "Volvo", "BMW ")
Note:If you need to specify a value or logical value in the array, the variable type should be a numeric or Boolean variable, rather than a character variable.
Access Array
By specifying the array name and index number, you can access a specific element.
The following is the code line:
Document. Write (mycars [0])
The output is as follows:
Saab
Modify values in an existing array
To modify the values in an existing array, you only need to add a new value to the specified label:
Mycars [0] = "Opel ";
Now, the above Code is as follows:
Document. Write (mycars [0]);
Output:
Opel