arrays are defined in four different ways
Using constructors:
var a = new Array ();
var B = new Array (8);
var c = new Array ("A", "second", "third");
or the direct amount of the array:
var d = ["A", "second", "third"];
Property
Array has only one property, that is, Length,length represents the number of memory space occupied by the array, not just the number of elements in the array, the B.length value is 8 < script > in the array just defined.
var a = new Array ("A", "second", "third")
A [48] = "12"
Document. Write (A. Length)
The result shown is 49
</script>
The length property of the array is writable, which is a very interesting attribute that we can use to intercept the array < script >
var a = new Array ("A", "second", "third")
Delete a [1]
Document. Write (A. Length)
The result shown is 3, which means that the length of the array cannot be changed even though it is deleted
var a = new Array ("A", "second", "third")
A. Length = 1
Document. Write (A. Length)
The result shown is 1, which means there is only one element left.
</script>
Method
There are no methods that include IE and FF that are not compatible:
ToString (): Converts an array to a string
toLocaleString (): Converts an array to a string
Join (): Converts an array into a symbolic concatenated string
Shift (): Moves an element of an array's head out
Unshift (): Inserts an element in the header of an array
Pop (): Deletes an element from the tail of an array
Push (): Adds an element to the tail of the array
Concat (): Adding elements to an array
Slice (): Returns the part of an array
Reverse (): Sort the array in reverse order
Sort (): Sorting operations on array
Splice (): Inserts, deletes, or replaces an array element
ToString () method, toLocaleString () method of the role is similar to the role of FF is exactly the same, ie if the element is a string, will be in the "," followed by a space, if the element is a number, will expand to two decimal places, Both change the length property of the string, so consider compatibility and try not to use the toLocaleString () method. < script >
var a = new Array (1, 2, 3, [4, 5, [6, 7]])
var B = A. ToString ()//b is a string of "1, 2, 3, 4, 5, 6, 7"
var c = new Array (1, 2, 3, [4, 5, [6, 7]])
var d = c. toLocaleString ()//d is a string of "1, 2, 3, 4, 5, 6, 7"
Both the ToString () method and the toLocaleString () method can disassemble a multidimensional array
</script>
The join () method converts all elements in an array to strings, and then joins together, which is exactly the opposite of the string's split () method. Join () By default is to use "," as a separator, of course, you can also specify the delimiter < script in the method >
var a = new Array ("A", "second", "third")
var s = A. Join ("..... " )
Document. Write (s)
The result shown is "First...second...third"
</script>
The Pop () method deletes several elements from the end of the array, and the push () method adds an element to the tail of the array, which is exactly two opposite operations. All two of them operate on the original array, but note that the push () method returns the length of the new array, while the Pop () method returns the deleted element. < script >
var a = new Array (1, 2, 3)
var B = A. Push (4, 5, [6, 7])//a for [1, 2, 3, 4, 5, [6, 7]] B for 6 note the push () method will not open an array for you
var c = new Array (1, 2, 3, 4, "a")
var d = c. Pop ()//c is [1, 2, 3, 4] D as a string of "a"
</script>
The shift () method deletes an element from the head of an array, and the Unshift () method adds several elements to the head of the array, which are exactly two opposite operations. All two are operations on the original array, but note that the Unshift () method returns the length of the new array, while the shift () method returns the deleted element. < script >
var a = new Array (1, 2, 3)
var B = A. Unshift (4, 5, [6, 7])//a for [4, 5, [6, 7], 1, 2, 3] B for 6 note the Unshift () method will not open an array for you, and the order in which the values are inserted.
var c = new Array ("A", 1, 2, 3, 4)
var d = c. Shift ()//c to [1, 2, 3, 4] D as a string of "a"
</script>
The Concat () method returns an array of elements added to the original array, separated by ",", and if there is an array in the element, it expands and continues to add, but does not support expanded add < script > in multidimensional array form
var a = new Array ("A", "second", "third")
s = A. Concat ("Fourth", ["Fifth", "sixth"], ["Seventh", ["Eighth", "ninth"]]
Document. Write (s [7])
The result shown is "eighth, ninth", stating that "eighth, ninth" is added in the form of an array, which is the value of S ["a", "second", "third", "fourth", "fifth", "sixth", "Seventh", ["Eighth", "ninth"]]
</script>
The slice () method returns a fragment of an array, or a child array. The slice () parameter represents the beginning and end of the word group, and if there is only one argument, it means to start from there until the last, and if there is a negative number, it indicates a position of the reciprocal. < script >
var a = new Array (1, 2, 3, 4, 5)
var B = A. Slice (3)//b is [4, 5]
var C = A. Slice (-3)//c is [3, 4, 5]
var d = A. Slice (1,-1)//d is [2, 3, 4]
var e = A. Slice (-3,-1)//e is [3, 4]
</script>
The reverse () method sorts the array in reverse, he does not create and return a new array, but rather operates on an existing array < script >
var a = new Array ("A", "second", "third")
A. Reverse ()
Document. Write (a)
The result is "Third,second,first", and the order of the arrays is reversed.
</script>
The
Sort () method is used to sort the array, which is a very peculiar method, and I don't know if the person who created him was lazy or smart, and that was a very impressive way for me. The parameter of the
sort () method is one with two arguments, and a function that returns a value, if the value returned is greater than 0, the previous argument is larger than the latter, equal to zero, and less than 0 indicates that the previous parameter is smaller than the latter, and the relatively small argument appears in the rank of the first. The
Sort () method operates directly on an array and returns a value, but the two appear to be equivalent. The sort () method is sorted by default in alphabetical order < script
var a = new Array (4, 543)
A. Sort (way)
function wa Y (x, y) {
if (x 2 = 0)
return 1;
If