There are four ways to define an array
Using constructors:
var a = new Array ();
var B = new Array (8);
var c = new Array ("First", "second", "third");
or array Direct volume:
var d = ["First", "second", "third"];
Property
Array has only one property, that is, Length,length represents the number of memory space that the array occupies, not just the number of elements in the array, in the array just defined, the value of B.length is 8
<script>
var a = new Array ("First", "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 property that we can use to intercept the array
<script>
var a = new Array ("First", "second", "third")
Delete A[1]
document.write (A.length)
The displayed result is 3, indicating that the length of the array cannot be changed even if it is deleted
var a = new Array ("First", "second", "third")
A.length = 1
document.write (A.length)
The result is 1, which means there's only one element left.
</script>
Method
This does not include some methods that are not compatible with IE and FF:
ToString (): Converts an array to a string
toLocaleString (): Converts an array to a string
Join (): Converts an array to a symbolic concatenated string
Shift (): Moves an element of the array head out
Unshift (): Inserts an element in the head of an array
Pop (): Deletes an element from the tail of the 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 backwards
Sort (): Sort operations on an array
Splice (): Inserting, deleting, or replacing an array element
The ToString () method, the toLocaleString () method acts similarly, the function under FF is exactly the same, ie if the element is a string, 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 as a string "1, 2, 3, 4, 5, 6, 7"
var c = new Array (1, 2, 3, [4, 5, [6, 7]])
var d = c.tolocalestring ()//d as a string "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 the elements in the array into strings, and then joins them, which is exactly the opposite of the split () method of String. Join () defaults to use "," as a delimiter, and of course you can also specify delimiters in the method
<script>
var a = new Array ("First", "second", "third")
var s = a.join ("...")
document.write (s)
The result of the display is "First...second...third"
</script>
The Pop () method can remove several elements from the end of an array, and the push () method adds an element to the end of the array, which is exactly two opposite operations. All two operate on the original array, but note that the push () method returns the length of the new array, and the Pop () method returns the element that was deleted.
<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 does not open an array for you
var c = new Array (1, 2, 3, 4, "first")
var d = c.pop ()//c for [1, 2, 3, 4] D as string "first"
</script>
The shift () method removes an element from the head of the 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 element that was deleted.
<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 is 6 note the Unshift () method does not open an array for you, and the order in which the values are inserted
var c = new Array ("First", 1, 2, 3, 4)
var d = c.shift ()//c for [1, 2, 3, 4] D as string "first"
</script>
The Concat () method can return an array of elements that have been added to the original array, separated by "," elements, and, if there is an array in the element, will be expanded and added, but not supported in the form of multidimensional arrays.
<script>
var a = new Array ("First", "second", "third")
s = A.concat ("Fourth", ["Fifth", "sixth"],["seventh", ["Eighth", "ninth"]])
document.write (S[7])
The result shown is "eighth, ninth", stating "eighth, ninth" is added in the form of an array, which is the value of S ["First", "second", "third", "fourth", "fifth", "sixth", "Seventh", ["Eighth", "ninth"]
</script>
The slice () method returns a fragment of an array, or a sub-array. The slice () parameter represents the beginning and end of the word group, and if there is only one argument, it will be taken from there until the last, and if the parameter has a negative number, it is 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 for [2, 3, 4]
var e = A.slice ( -3,-1)//e for [3, 4]
</script>
The reverse () method reverses the array and does not create and return a new array, but instead operates on the original array.
<script>
var a = new Array ("First", "second", "third")
A.reverse ()
document.write (a)
The result of the display is "Third,second,first", when the order of the arrays has been reversed
</script>
The sort () method works by sorting the arrays, which is a very peculiar approach, and I don't know if the person who created him was lazy or smart, which is an impressive approach.
The parameter of the sort () method is a function with two parameters, and has a return value, if the value returned is greater than 0, then the previous parameter is larger than the latter one, equals zero is equal, less than 0 indicates that the previous parameter is smaller than the last one, and the relatively small one appears at the top of the sort.
The sort () method operates directly on the array and also 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 (33, 4, 111, 543)
A.sort (To)
function (x, y) {
if (x% 2 ==0)
return 1;
if (x% 2!=0)
return-1;
}
The result of sorting is to make an odd number in the front even after
</script>
The function of the splice () method is to insert, delete, or replace an array element, and he will not only modify the original array, but also return the processed content, so this is a powerful, but not easy to use method, the splice () method is positioned with the first two parameters, The remaining parameters represent the insertion section.
<script>
var a = new Array (1, 2, 3, 4, 5)
var B = A.splice (2)//a for [1, 2] b for [3, 4, 5]
var c = new Array (1, 2, 3, 4, 5)
var d = c.splice (2,2)//c for [1, 2, 5] D for [3, 4]
var e = new Array (1, 2, 3, 4, 5)
var f = f.splice ( -4,2)//e for [1, 4, 5] f for [2, 3]
var g = new Array (1, 2, 3, 4, 5)
var h = g.splice ( -2,-2)//The second parameter represents length, so negative numbers are invalid here
var i = new Array (1, 2, 3, 4, 5)
var j = i.splice (2,2, "first", "second", "third")//i for [1, 2, "first", "second", "third", 5] J for [3, 4] The rear part will automatically move back and forth to preserve the continuity of the array
var k = new Array (1, 2, 3, 4, 5)
var L = k.splice (2,2,["First", "second"], "third")//k for [1, 2, ["First", "second"], "third", 5] L for [3, 4] the splice () method does not expand the array, only Write directly
</script>
JavaScript array definition