JS Array definition

Source: Internet
Author: User
Tags array definition

JS Array definition Collection

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 an 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 incompatible 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 (): Add an element to the tail of the array
Concat (): Add to the array? element
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 the FF is exactly the same, ie words assume that the element is a string, will be "," followed by a space, assuming that 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 are capable of disassembling multidimensional arrays
</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 removes several elements from the tail of the array, and the push () method adds an element to the end of the array, which is exactly two opposite. 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, assumed to have an array, and will be expanded and continued to join?, but does not support the expansion 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 as an array? In, this is the value of S for ["first", "second", "third", "fourth", "fifth", "sixth" , "Seventh", ["Eighth", "ninth"]]
</script>
The slice () method returns a fragment of an array, or a sub-array. The number of slice () indicates the beginning and end of the word group, assuming that there is only one parameter, which means that it starts from there until the last, assuming a negative number for the reference, indicating 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 way, and I don't know if the person who created him was lazy or smart, which is an impressive approach.
The sort () method is a function that has two parameters and has a return value, assuming that the returned value is greater than 0, then the previous parameter is greater than the last one, equals zero, and less than 0 indicates that the previous one is smaller than the last, and the relatively small one is the current rank.
The sort () method operates directly on the array and returns values at the same time, 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, he will not only change the original array, but also return the processed content, so this is a powerful, but not easy to use method, Splice () method with the first two parameters to locate, The remaining number of references represents the Insert 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)//second parameter indicates 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 later part will be moved back and forth to keep the array continuous Of
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 only
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.