JS array Definition

Source: Internet
Author: User
Tags array definition

Arrays can be defined in four ways.
Use constructors:
Var a = new Array ();
Var B = new Array (8 );
Var c = new Array ("first", "second", "third ");
Or the number of arrays directly:
Var d = ["first", "second", "third"];

Attribute

Array has only one attribute, that is, length. length indicates the number of memory space occupied by the Array, not just the number of elements in the Array. In the Array just defined, B. the value of length is 8.

<Script>
Var a = new Array ("first", "second", "third ")
A [48] = "12"
Document. write (a. length)
// The result is 49.
</Script>
The length attribute of the array is writable, which is a very interesting attribute. We can use this method to intercept the array.

<Script>
Var a = new Array ("first", "second", "third ")
Delete a [1]
Document. write (a. length)
// The 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, indicating that only one element is left.
</Script>
Method

There are no methods that are incompatible with IE and FF:
ToString (): converts an array into a string.
ToLocaleString (): converts an array into a string.
Join (): converts an array into a string connected with symbols.
Shift (): removes an element from the array header.
Unshift (): insert an element in the header of the array.
Pop (): deletes an element from the end of the array.
Push (): Add an element to the end of the array.
Concat (): add elements to the array
Slice (): returns the part of the array
Reverse (): sorts arrays in reverse order.
Sort (): sorts arrays.
Splice (): insert, delete, or replace an array element.

ToString () method. The toLocaleString () method works similarly. FF works exactly the same. For IE, if the element is a string, a space is added after, if the element is a number, it will be extended to two decimal places, both of which will change the length attribute of the string, so considering compatibility, 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 in the string format 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 in the string format of "1, 2, 3, 4, 5, 6, 7"
// Both the toString () method and the toLocaleString () method can disassemble multi-dimensional arrays.
</Script>
The join () method converts all elements in the array into strings and connects them. This is exactly the opposite of the split () method of String. By default, join () uses "," as the separator. You can also specify the Separator in the method.

<Script>
Var a = new Array ("first", "second", "third ")
Var s = a. join ("...")
Document. write (s)
// The displayed result is "first... second... third"
</Script>
The pop () method can delete several elements from the end of the array. The push () method adds an element to the end of the array. The two methods are just two opposite operations. Both operations are performed 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 =. push (, []) // a is [1, 2, 3, 4, 5, [6, 7] B is 6 Note push () method will not open an array for you
Var c = new Array (1, 2, 3, 4, "first ")
Var d = c. pop () // c is [1, 2, 3, 4] d in the string format of "first"
</Script>
The shift () method can delete an element from the array header. The unshift () method adds several elements to the array header. These two methods are just two opposite operations. Both operations are performed 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 =. unshift (, []) // a is [4, 5, [6, 7], 1, 2, 3] B is 6 Note unshift () the method will not open an array for you, and the order of the inserted values
Var c = new Array ("first", 1, 2, 3, 4)
Var d = c. shift () // c is [1, 2, 3, 4] d in the string format of "first"
</Script>
The concat () method returns an array of elements added to the original array. The elements are separated by commas (,). If an array exists in the element, it is expanded and added, but it does not support multi-dimensional array expansion and addition.

<Script>
Var a = new Array ("first", "second", "third ")
S = a. concat ("fourth", ["th", "sixth"], ["seventh", ["eighth", "ninth"])
Document. write (s [7])
// The displayed result is "eighth, ninth", indicating that "eighth, ninth" is added as an array. The value of s is ["first ", "second", "third", "fourth", "th", "sixth", "seventh", ["eighth", "ninth"]
</Script>
The slice () method returns a piece of the array, or a sub-array. The slice () parameter indicates the start and end position of the word group. If there is only one parameter, it indicates that it is obtained from the beginning to the end. If there is a negative number, it indicates a reciprocal position.

<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 arrays in reverse order. Instead of creating and returning a new array, it operates on the original array.

<Script>
Var a = new Array ("first", "second", "third ")
A. reverse ()
Document. write ()
// The result is "third, second, first". At this time, the order of the array has been reversed.
</Script>
The sort () method is used to sort arrays. This is a very strange method. I don't know whether the person who created it was out of laziness or intelligence, this is an impressive method.
The parameter of the sort () method is a function with two parameters and return values. If the returned value is greater than zero, it indicates that the previous parameter is greater than the previous one. If it is equal to zero, it is equal, if the value is smaller than zero, the first parameter is smaller than the last one, and the smaller one will appear at the top of the sorting list.
The sort () method operates directly on the array and returns the value, but the two seem to be equivalent. By default, the sort () method is sorted by letters.

<Script>
Var a = new Array (33, 4,111,543)
A. sort (way)
Function way (x, y ){
If (x % 2 = 0)
Return 1;
If (x % 2! = 0)
Return-1;
}
// The result of sorting is to make the odd number first and even number behind
</Script>
The splice () method inserts, deletes, or replaces an array element. It not only modifies the original array, but also returns the processed content, therefore, this is a powerful method, but it is not easy to use. The splice () method uses the first two parameters for locating, and the remaining parameters represent the inserted part.

<Script>
Var a = new Array (1, 2, 3, 4, 5)
Var B = a. splice (2) // a is [1, 2] B is [3, 4, 5]
Var c = new Array (1, 2, 3, 4, 5)
Var d = c. splice () // c is [1, 2, 5] d is [3, 4]
Var e = new Array (1, 2, 3, 4, 5)
Var f = f. splice (-) // e is [1, 4, 5] f is [2, 3]
Var g = new Array (1, 2, 3, 4, 5)
Var h = g. splice (-2,-2) // The second parameter indicates the length, so the negative number is invalid here

Var I = new Array (1, 2, 3, 4, 5)
Var j = I. splice (2, 2, "first", "second", "third") // I is [1, 2, "first", "second", "third ", 5] The part after j is [3, 4] is automatically moved forward and backward to maintain the continuity of the array.
Var k = new Array (1, 2, 3, 4, 5)
Var l = k. splice (2, 2, ["first", "second"], "third") // k is [1, 2, ["first", "second"], "third", 5] l is [3, 4] The splice () method does not expand the array and only writes data directly.
</Script>

Related Article

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.