Javascript array object

Source: Internet
Author: User
Tags javascript array

New array ()
New array (LEN)
New array ([item0, [Item1, [item2,...]

Method to use an array object:

VaR objarray = new array ();

Objarray. concact ([Item1 [, item2 [,...] ------------------- connect the parameter list to the end of objarray to form a new array and return it. The original array is not affected. For example, VAR arr = ["A", "B", "C"];
Arr. concact ("D", "E ");
Returns an array containing the letter elements from "A" to "E. The arr itself is not affected.

Objarray. Join (separator) ----------------- converts an array into a string using the character specified by separator as a delimiter. When seperator is a comma, it acts the same way as tostring.

Objarray. Pop () --------- in general, it is the last element of the pop-up array. The following push method makes it possible to use arrays as stacks. The pop method returns the value of the last element of the array and reduces the Length attribute by 1. This means that the last element is lost immediately after the return result.

Objarray. Push ([value1 [, value2 [,...]) ----------- Add the parameter to the end of the array. For example, [1, 2, 3, 4]. Push ("A", "B") will get [1, 2, 3, 4, "A", "B"]

Objarray. Reverse () sorts the elements in the array in reverse order. For example, [, 3]. Reverse () will get [, 1]. This operation is performed on the original array, and the array itself is also returned.
Objarray. Shift () ----------- removes the first element of the array and returns the value of this element. This method is similar to the pop method. The pop method removes the last element.

Objarray. Slice (START, end) ----------- returns a subset of the array object. The index starts from start (including START) to end (excluding end), and the original array is not affected. For example, [,]. Slice () produces [, 4]. When 'start' or 'end' is a negative number, use them to add the 'length' value. For example, [1, 2, 3, 4, 5]. Slice (-4,-1) will get [3, 4, 5]. If end is less than or equal to start, an empty array is returned.

Objarray. Sort (comparefn) ------- sorts an array based on the size comparison function defined by comparefn. The comparefn function must accept two parameters: element1 and element2. If you want element1 to be placed before element2, a negative number is returned. If you want element1 to be placed after element2, a positive number is returned, if the two numbers are treated equally (that is, the original order is maintained), 0 is returned. When comparefn is omitted, the elements are arranged alphabetically. For example, for the defined comparison function CMP: function CMP (E1, E2) {return E1-E2;} Then [,]. Sort (CMP) will get [,].
Objarray. splice (START, deletecount [, Item1, item2 [,...]) is a complex function used to delete, replace, and insert array elements. The start parameter indicates the index location for the operation, and the deletecount parameter indicates the number of elements to be deleted from the start (including the start position). If deletecount is omitted, the remainder of the array is to be deleted from start. [, Item1 [, item2 [,...] indicates an optional list of elements inserted before start. For example:

VaR arr = [0, 1, 2, 3, 4, 5, 6];

Arr. splice (1, 1 );

Document. Write (ARR); // displays"

Arr = [0, 1, 2, 3, 4, 5, 6];

Arr. splice (0, 0, "A", "B ");

Document. Write (ARR); // displays "a, B, 6"

Arr = [0, 1, 2, 3, 4, 5, 6];

Arr. splice (3,2, "C", "D ");

Document. Write (ARR); // display ", 2, c, d, 5, 6"

Objarray. unshift (Item1 [, item2 [,...]) ----------------- Insert the parameter list to the beginning of the array. Its nature and push method type, but the push method adds elements to the end of the array. For example, [1, 2, 3, 4]. unshift ("A", "B") Returns ["A", "B", 1, 2, 3, 4].

From Bubble Sorting:

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en" >
< Html >
< Head >
< Title > New Document </ Title >
< Meta Name = "Generator" Content = "Editplus" >
< Meta Name = "Author" Content = "" >
< Meta Name = "Keywords" Content = "" >
< Meta Name = "Description" Content = "" >
</ Head >

< Body >
< Script Language = "JavaScript" Type = "Text/JavaScript" >
<! --
Function Bubblesort (ARR ){
// The outer loop. Arr. length is used to calculate the maximum value.
For ( VaR I = 0 ; I < Arr. length; I ++ ){
// The inner layer loops, finds the I-th element, and exchanges it with the I-th element.
For ( VaR J = I; j < Arr. length; j ++ ){
If (ARR [I] < Arr [J]) {
// Swap the positions of two elements
VaR Temp = Arr [I];
Arr [I] = Arr [J];
Arr [J] = Temp;
}
}
}
}

VaR Arr = [ 32 , 55 , 78 , 43 , 78 , 10 , 45 , 20 , 9 , 89 ];
Bubblesort (ARR );

// Output :,
For ( VaR I = 0 ; I < Arr. length; I ++ ){
Document. Write (ARR [I] + " , " );
}
// -->
</ Script >

</Body>
</Html>

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.