1. Get to know arrays
An array is a collection of certain types of data. The data type can be integer, string, or even object.
Javascript does not support multi-dimensional arrays, but because arrays can contain objects (an array is also an object), arrays can achieve functions similar to multi-dimensional arrays by nesting each other.
1.1 Define array
Declare an array with 10 elements:
As mentioned above, objects can be placed in arrays, for example, the following code:
Arrays can also be assigned values directly when instantiated, for example:
a and b are both arrays, but b uses an implicit declaration to create another instance. At this time, if alert(a==b) is used, false
will pop up1.2 Multidimensional Array
Actually, Javascript does not support multi-dimensional arrays. In asp, you can use dim a(10,3) to define multi-dimensional arrays. In Javascript, if you use var a = new Array(10,3), an error will be reported
But as mentioned before, arrays can contain objects, so you can declare an element in the array as an array, for example:
The effect is the same, a uses regular instantiation, b is an implicit declaration, and the result is a multi-dimensional array.
1.3 Array literals
I really don’t know what this is called in Chinese, text array?
Speaking of arrays, we have to talk about Array Literals. Arrays are actually special objects. Objects have unique properties and methods. Values and calls are obtained through object name.property, object.method(), and arrays are obtained through the following Mark to get the value. Array Literals are similar to arrays in many ways. They are both collections of a certain data type. However, Array Literals is fundamentally an object. Its declaration and call are different from arrays:
Create a simple object. Generally, the call is through aa.x. If it is used as Array literals, using alert(aa["x"]) will also pop up cat
This is another way to create an object, the result is the same
2. Operation of array elements
As mentioned above, you can read and write elements through array [subscript]
The range of the subscript is 0 – (23 (superscript 2) -1), and the subscript is a negative number, floating point or even a Boolean value time, the array will be automatically converted to the object type, for example:
This is equivalent to b["2.2"] = "XXXXX".
2.1 Array loop
Still pops up 1 to 6 in sequence. for...in is a traversal object (array is a special object) object. It is used on an array. Because the array has no attribute name, the value is output directly. This structural statement is used on the object. For example:
At this time, e gets the attribute name, that is, x, y, x. To get the value, the array name [attribute] is used, so a[e] is equivalent to a["x"], a[" y”], a[“z”]
2.2 Common array functions
concat
Appends an array after the existing array and returns the new array without affecting the existing array:
It should be noted that it can only be used for arrays or strings. If the connection (a in front) is a numerical value, Boolean value, or object, an error will be reported. When a string is connected to an array, the string will be followed by the first element of the array. Splicing into new elements, while the array connection string will append new elements (I don't know the reason for this, please disclose if you know). For arrays containing arrays and objects, they will remain intact after connection.
join
Concatenate the arrays with specified separators and convert the arrays into strings:
This is easy to understand, but it should be noted that only one-dimensional arrays are converted. If there are arrays in the array, the string connection specified by join will not be used, but the default toString() will be used, such as
Note: The array inside the array is not used for * connection
pop
Delete the last element of the array and return the element
Note: If the array is empty, undefined is returned
push
Add an array to the end of the array and return the new length of the array
The difference with concat is that concat does not affect the original array and directly returns a new array, while push directly modifies the original array and returns the new length of the array
sort
Array sorting, let’s look at an example first
Isn’t the result surprising? Yes, the sorting is not by integer size, but by string comparison. That is to compare the ANSI code of the first character. The smaller one is ranked first. If it is the same, the second character is taken and then Ratio, if you want to compare by integer values, you can do this
The sort() method has an optional parameter, which is the function in the code. This is a simple example. Non-numbers cannot be sorted. Non-numbers require more judgment, so I won’t go into details here
reverse
Reverse sorting of the array is the same as sort(), taking the ASCII value of the first character for comparison
If the array also contains an array, it will be treated as an object and the elements will not be extracted
Logically, it should be at the end of row 11, because 4, 11, 33 are compared here as complete objects, so they are ranked first
If you don’t understand, use join() to string them together, and it will be much clearer.
shift
Delete the first element of the array and return the element, which is similar to pop
Note: When the array is empty, undefined is returned
unshift
In contrast to shift, add elements to the front of the array and return the new length of the array
Note that this method will return undefined under IE, which seems to be a Microsoft bug. I can correctly use the new length of the array under Firefox
slice
Return array fragment
a.slice(1,2), the number starting from subscript 1 to subscript 2, note that the element with subscript 2 is not included
If there is only one parameter, it defaults to The last
-4 of the array represents the 4th element from the bottom, so the four elements from the bottom are returned. The last row of
starts from the 2nd from the bottom. Because it is intercepted backward, it is obvious that the previous elements cannot be obtained, so Returns an empty array. If changed to a.slice(-6,-2), it returns b,c,d,e
splice
Delete an element of a fragment from the array and return the deleted element
Note that the second parameter of this method is required under IE. If not filled in, it defaults to 0, such as a.splice(4). Under IE, it returns empty, and the effect is equivalent to a.splice(4,0 )
toString
Convert arrays to strings, not just arrays, but all objects can use this method
Boolean value returns true or false, object returns [object objectname]
Compared with join() method, join() only replaces one-dimensional array, while toString() replaces the entire array (regardless of one-dimensional array). Dimensional or multi-dimensional) completely flat
At the same time, this method can be used for decimal, binary, octal, and hexadecimal conversion, for example:
Conversion can only be performed on elements. If the entire array is converted, the array will be returned unchanged
toLocaleString
Returns a local format string, mainly used on Date objects
The difference is that toString() returns the standard format, and toLocaleString() returns the complete date in the local format (in [Control Panel]>>[Regional and Language Options], by modifying the [Time] and [Long Date] formats) , toLocaleDateString() is the same as toLocaleString(), but it takes less time
valueOf
Returns different original values according to different objects. When used for output, it is similar to toString(), but toString() returns the string type, while valueOf() returns the original object type
Arrays are also objects, so typeof (a.valueOf()) returns object, which is still a multi-dimensional array
The Date object returns the number of milliseconds since January 1, 1970. The Math and Error objects do not have the valueOf method.