Home > Article > Web Front-end > JavaScript Array Example Tutorial
An array is a collection of many variables with the same name. The usage of an array is exactly the same as that of an ordinary variable. It can also be stored in any data type. The only difference is that it occupies a continuous space in the memory. You can number them sequentially and use them accordingly. The previous article [Usage of JavaScript Arrays] mainly introduces the use of js arrays on the Internet. This article introduces the powerful functions and usage methods of js arrays through examples.
Arrays are made up of many variables with the same name. Arrays The usage is exactly the same as that of ordinary variables. It can also be stored in any data type. The only difference is that it occupies a continuous space in the memory. You can number them in sequence and use them according to the number. . The benefit of an array is that it can declare and use multiple variables at once. The method of using JAVAScript arrays is different from that of VBScript. When using it, it should be enclosed in square brackets "[]", and different variables should be separated by commas ",".
var array name;
array name = [1,...,n];
Example: I want to define an array "fruit", which contains three fruits: "watermelon", "apple", and "banana" , we have to write like this:
var fruit;
fruit = ["watermelon", "apple", "banana"]; //Assign values to the three fruits in the array
At this time, "fruit[0]" is " "Watermelon", "fruit[1]" is "apple", "fruit[2]" is "banana", and "fruit" is "watermelon, apple, banana". (JAVAScript will start counting from "0", this principle must be remembered.)
If you want to reassign values to the variables in the array, such as changing "apple" to "strawberry", you should write like this:
fruit = [" Watermelon","Apple","Straw mold"]; //Reassign the variables in the array "fruit"
Because the variables "fruit[0]" and "fruit[2]" in the array remain unchanged, you can also Write like this:
fruit = [fruit[0], fruit[1], "grass mold"]; //Change the variable "fruit[2]" in the "fruit" array to "grass mold"
Note: Even if the variable " The values of "fruit[0]" and "fruit[2]" will not be changed, but they must be written, otherwise the original values will be lost and become "undefined".
In fact, you don’t need to write out all the variables of the array. If you write the array like this:
var fruit;
fruit = ["watermelon", ,"strawberry"]; //Assign values to the three fruits in the array
This When "fruit[0]" is "watermelon", "fruit[1]" is "undefined", "fruit[2]" is "grass mold", and "fruit" is "watermelon,,grass mold".
If you write this array like this:
var fruit;
fruit = ["watermelon", "banana", ,]; //Assign values to the four fruits in the array
At this time, there are four variables in the array "fruit[0 ]" is "watermelon", "fruit[1]" is "straw mold", "fruit[2]" is "undefined", "fruit[3]" is "undefined", "fruit" is "watermelon, straw mold" ,,".
The following is an example:
Note: JAVAScript array and VBScript Arrays are not only different in syntax, but also have many differences when used. Please pay attention to the distinction.
1. Create array object
We will introduce how to create array objects in JAVAScript. (Actually, this method is essentially the same as the previous method, except for the way the statements are written. The above method is simpler to use when the program is very short. In general, I still recommend that you use the following method. Create an array object.) There are two syntaxes for creating an array object:
1. When declaring an array, only declare several components in the array.
var array object name = new Array (number of components);
fruit = new Array(3); //Declare an array named fruit. There are three components in total. This is equivalent to declaring three variables at once
Then you must Prepare a few additional lines of program code and fill in the variable values sequentially.
fruit[0] = "watermelon";
fruit[1] = "apple";
fruit[2] = "banana";
2. Directly give all array components when declaring, separated by commas, Surrounded by parentheses, the number of components is the length of the array.
var array object name = new Array (component one..., component N);
var fruit = new Array ("watermelon", "apple", "banana");
Note: In general languages, the components in the array must are values of the same type, but different types of data can be put into arrays in JAVAScript.
2. Attributes of array objects
JAVAScript provides the following attributes for array objects:
Usage format:
Array object name.Attributes
Ordered attribute name usage instructions
1 constructor specifies the function to create a prototype prototype
2 index represents the index value of the array component
3 input represents the string in the regular expression.
4 length gets the length of the array (number of array components).
5 prototype is used to create custom object properties
3. Methods of array objects
JAVAScript provides the following methods for array objects:
Usage format:
Array object name.Method (parameter)
Ordered method name usage Description
1 concat(array1,array2,…,arrayN) combine multiple arrays into a new array
2 join(separator character) combine arrays into a string, separated by specific characters
3 pop( ) Delete the last component in the array and return the content of the component
4 push (component 1, component 2,..., component N) Add one or more components to the end of the array and return the content of the last component
5 reverse () Reverse the index order of all components in the array (Transpose)
The first component becomes the last one, and the last component is brought to the front
6 shift() Delete the first component in the array and return the Component content
7 slice (start index, end index) Transfer the array content into a new array
8 sort() Sort the array content
9 splice() Add or delete array components
10 toSource() Return the specific array The array constant can be used to create a new array
11 toString() represents the array and its components as a string
12 unshift(component 1, component 2,..., component N) adds one or more components to the array The front, and returns the last array length
13 valueOf() Gets the array value
Note: Some of these methods, such as push, shift, unshift... are not supported by some versions of IE browsers, so you should pay special attention when using them.
Example:
3. Two-dimensional array
JAVAScript’s array object is actually only one-dimensional structure, but we can use the further design and utilization of one-dimensional arrays to put the array into the array, so that the components in the array are also arrays, forming a two-dimensional array in JAVAScript. However, errors are prone to occur in the use of two-dimensional arrays, so we will only introduce its concept here and do not recommend its use. The above is the content of the JavaScript array example tutorial. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!