Array type of JS

Source: Internet
Author: User
• Variable-length array object
-0-based
-Maximum length: 2 ^ 32 −1 = 4294967295
• Construct an array object
-New array (): empty array
-New array (3): An array with a length of 3
-New array (1, "hello"): constructs an array with two elements

Array. prototype. ...
• Length attribute: array length, read/write
• Tostring ()/tolocalestring () method:
-Returns a comma-separated string.
-The difference between the two is the way to get each object string
• Concat ([Item1 [, item2 [,... ]) Method:
-Return a new array to save all elements and parameters of the original array.
• Push ([Item1 [, item2 [,... ]) Method:
-Add one or more elements at the end of the array.
• Pop method:
-Remove and return elements from the end of the array
• Shift method:
-Remove and return elements from the array Header
• Unshift ([Item1 [, item2 [,... ]) Method:
-Add one or more elements to the array header.
• Join (separator) method:
-Returns a string that uses separator as the delimiter to connect all elements.
-The basis of stringbuilder can accelerate String concatenation for IE
• Reverse () method: reverse all elements in the array
• Sort (comparefunction) method:
-The parameter is a method used to compare two elements.
-If the parameter is omitted, use <, =,> to compare two elements.
• Slice (START, end) method:
-The new array is returned, which does not affect the old array.
-Contains elements starting from subscript start to subscript end-1
-If end is omitted, all elements starting from the subscript start to the end are included.
-If the parameter is negative, it indicates the subscripts (I .e., the subscript is n + array. Length) of the array's "reciprocal)
-If the start element is after the end element or is the same, an empty array is returned.
-If the parameter is negative, it indicates the subscripts (I .e., the subscript is n + array. Length) of the array's "reciprocal)
-If the start element is after the end element or is the same, an empty array is returned.
• Splice (START, deletecount [, Item1 [, item2 [,... ]) Method:
-The most flexible method affects the current array
-Delete the deletecount element from the element of the subscript start and insert the remaining element at the current start position.
-Delete element: splice (2, 1)
-Insert element: splice (2, 0, "hello", "world ")
-Replacement element: splice (2, 1, "hello", "world ")

Html < Div ID = " Info " > </ Div >
< Script Language = " Javascript " Type = " Text/JavaScript " >
Function display (text)
{
Document. getelementbyid ( " Info " ). Innerhtml + = (Text +   " <Br/> " );
}

Array. Prototype. Display = Function ()
{
Window. Display ( " A: "   +   This );
}

VaR =   New Array ( 1 , 2 , 3 , 4 , 5 ); // A = [1, 2, 3, 4, 5];
Display ( " Array initialized " );
A. Display ();

Display ( " A. Push (6, 7) " );
A. Push ( 6 , 7 );
A. Display (); // [1, 2, 3, 4, 5, 6, 7]

Display ( " A. Shift (): "   + A. Shift ());
A. Display (); // [2, 3, 4, 5, 6, 7]

Display ( " A. unshift (8) " );
A. unshift ( 8 );
A. Display (); // [8, 2, 3, 4, 5, 6, 7];

Display ( " A. Pop (); "   + A. Pop ());
A. Display (); // [8, 2, 3, 4, 5, 6];

Display ( " A. Concat (9, 10 ): "   + A. Concat ( 9 , 10 )); // [8, 2, 3, 4, 5, 6, 9, 10];
A. Display (); // [8, 2, 3, 4, 5, 6]

Display ( " '[' + A. Join ('] [') + ']': "   +   ' [ '   + A. Join ( ' ] [ ' ) +   ' ] ' ); // [8] [2] [3] [4] [5] [6];

Display ( " A. Slice (2, 5 ): "   + A. Slice ( 2 , 5 )); // [3, 4, 5]
Display ( " A. Slice (2,-2 ): "   + A. Slice ( 2 , - 2 )); // [3, 4]
A. Display ();

Display ( " A. splice (3, 2) " );
A. splice ( 3 , 2 );
A. Display (); // [8, 2, 3, 6]

Display ( " A. splice (2, 0, 7, 8) " );
A. splice ( 2 , 0 , 7 , 8 );
A. Display (); // [8, 2, 7, 8, 3, 6]

Display ( " A. splice (3, 1, 9, 10) " );
A. splice ( 3 , 1 , 9 , 10 );
A. Display (); // [8, 2, 7, 9, 10, 3, 6]

Display ( " A. Reverse () " );
A. Reverse ();
A. Display (); // [6, 3, 10, 9, 7, 2, 8]

Display ( " A. Sort () " );
A. Sort ();
A. Display (); // [10, 2, 3, 6, 7, 8, 9]

Display ( " A. Sort (function (x, y) {return y-X ;}); " );
A. Sort (function (x, y ){ Return Y - X ;});
A. Display (); // [10, 9, 8, 7, 6, 3, 2];
</ 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.