Suggested reading time: 10 minutes
Main content: Basic concepts, properties, methods of JavaScript arrays
New array:
var arr01 = ["A", "B", "C", "D"]; var New Array ("A", "B", "C", "D");
The above two methods can create arrays ["A", "B", "C", "D"];
The difference: Arr02 is created through the array class, and Arr01 is created by assigning values in the literal way provided by JS.
You can receive two types of parameters in the new Array (), the first: the inner fabric value of the array (as in the above code), and the second: a number that specifies the length of the array (an empty array);
Values and assignments of arrays
Value: "A" in Arr01 can be obtained by subscript arr01[0]
Assignment: by subscript arr01[0] = "M" You can modify the "a" of the Arr01 species to "M"
If the subscript is larger than the original length, such as arr01[6] = "K", then the length of the array becomes 7, where the 5 and 6 bits are assigned undefined
The length of the array
Get through Arr01.length
can also be changed by length assignment, such as arr01.length=3, and modified length
When the length of the modification is less than the original length, it will delete the extra subscript on the value, if more than the original length, the extra length of the part will be assigned undefined
Traversal of an array
General for Loop
for (var i=0; i<arr01.length; i++) { console.log (arr01[i]); }
For In loop
for inch Arr01) { console.log (Arr01[index]); }
Two loops to get all the values of the array
Difference: The first one can loop out all the underlying values, including the value of the underlying that is not assigned (undefined), the second loop has a worthwhile part, and the subscript without the assignment is not looped
Common Methods for arrays:
Join (): Put all the elements of an array into a string, separated by a delimiter
Pop (): Delete and return the last element of the array
Push (): Adds one or more elements to the end of the array and returns the new length
Shift (): Delete and return the first element of the array
Unshift (): Adds one or more elements to the beginning of the array and returns the new length
Reverse (): Reverses the order of the elements in the array
Sort (): Sorts the elements of an array
Concat (): Joins two or more arrays and returns the result
Slice (): Returns the selected element from an existing array
Splice (): Delete the element and add a new element to the array
Multidimensional Arrays:
In fact, it is the array that contains the arrays internally. Its operation is exactly the same as a one-dimensional array, such as the following two-dimensional array:
[ ["NAME0", "Age0", "Address0"], ["Name1", "Age1", "Address1"], ["Name2", " Age2 "," Address2 "]
Finish!
10 minutes to get to the JavaScript array