Javascript Array class Arrays operation method _javascript tips

Source: Internet
Author: User
Tags javascript array
Push method
Adds a new element to an array and returns the new length value of the array.
Arrayobj.push ([Item1 [item2 [...] [Itemn]]])
Parameters
Arrayobj required option. An Array object.
Item, Item2,... itemn option. The new element of the Array.
Description
The push method adds these elements in the order in which the new elements appear. If one of the arguments is an array, the array is added as a single element to the arrays. If you want to merge elements from two or more arrays, use the Concat method.
Example
<script type= "Text/javascript" > var arrayobj = new Array (0,1,2,3,4); Arrayobj.push (5,6,7,8, "Maple Rock", "Cnlei"); Alert (arrayobj[arrayobj.length-1]); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Requirements
Version 5.5
Pop Method
Moves the last element in the divisor group and returns the element.
Arrayobj.pop ()
The required arrayobj reference is an Array object.
Description
If the array is empty, then the undefined is returned.
Example
<script type= "Text/javascript" > var arrayobj = new Array (0,1,2,3,4); Alert (Arrayobj.pop ()); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Requirements
Version 5.5
Shift Method
Moves the first element in the divisor group and returns the element.
Arrayobj.shift ()
Parameters
The required arrayobj reference is an Array object.
Description
The shift method moves the first element in the divisor group and returns the element.
Requirements
Version 5.5
Unshift Method
Inserts the specified element into the starting position of the array and returns the array.
Arrayobj.unshift ([item1[, item2 [, ...] [, Itemn]]]
Parameters
Arrayobj required option. An Array object.
Item1, item2,.. ., itemn Optional. The element that will be inserted at the beginning of the Array.
Description
The Unshift method inserts these elements into the beginning of an array, so these elements appear in the order in the sequence of arguments in the array.
Requirements
Version 5.5
concat Method (Array)
Returns a new array, which is a combination of two or more arrays.
Array1.concat ([item1[, item2[, ...) [, Itemn]]]
Parameters
Array1 required option. The array object to which all other arrays are to be connected.
Item1,.. ., itemn Optional. Other items to connect to the end of Array1.
Description
The Concat method returns an Array object that contains a connection to the array1 and any other items provided.
Items to add (item1 ... itemn) are added to the array in Left-to-right order. If an item is an array, add its contents to the end of the array1. If the item is not an array, it is added to the end of the array as a single array element.
The following is a copy of an element from the source array to the result array:
For object parameters copied from the array being connected to the new array, the same object is still pointed to after replication. Whatever changes in the new array and source array will cause another change.
For a numeric or string connection to a new array, only its value is copied. A change in the value of an array does not affect the value in the other array.
Example
The following example illustrates the use of the Concat method when using an array:
Copy Code code as follows:

function Concatarraydemo () {
var A, B, C, D;
A = new Array (1,2,3);
b = "JScript";
c = new Array ("VBScript");
D = A.concat (b, c);
Returns an array of [1, 2, 3, JScript, "," VBScript "]
return (d);
}

Requirements
Version 3
Join Method
Returns a string value that contains all the elements of an array that is connected together, separated by the specified delimiter.
Arrayobj.join (separator)
Parameters
Arrayobj required option. The Array object.
Separator Required option. is a string object that acts as a separator between the elements of an array in the final String object. If this argument is omitted, the array elements are separated by a comma.
Description
If an element in the array is not defined or NULL, it is treated as an empty string.
Example
The following example illustrates the use of the Join method.
Copy Code code as follows:

function Joindemo () {
var a, B;
A = new Array (0,1,2,3,4);
b = A.join ("-");
return (b);
}

Requirements
Version 2
Sort Method
Returns an Array object in which an element has been sorted.
Arrayobj.sort (sortfunction)
Parameters
Arrayobj required option. Any Array object.
Sortfunction can be selected. is the name of the function used to determine the order of elements. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.
Description
The sort method sorts the array object appropriately, and does not create a new array object during execution.
If you provide a function for the sortfunction parameter, the function must return one of the following values:
(1) A negative value if the first argument passed is smaller than the second argument.
(2) 0 if two parameters are equal.
(3) Positive value if the first argument is larger than the second parameter.
Example
<script type= "Text/javascript" > Function ascsort (x, y) {ret Urn x = = y? 0: (x > Y 1:-1); function Descsort (x, y) {return x = = y? 0: (x > Y -1:1); function Randomsort (x, y) {return Math.floor (Math.random () * 2-1); var array = [2,4,3,5,1,6,9,0,8]; document.write ("<p> Positive order:" + array.sort (ascsort) + "</p>"); document.write ("<p> Reverse:" + array.sort (descsort) + "</p>"); document.write ("<p> random Sort:" + array.sort (randomsort) + "</p>"); document.write ("<p> random Sort:" + array.sort (randomsort) + "</p>"); document.write ("<p> random Sort:" + array.sort (randomsort) + "</p>"); </script>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform]

Requirements
Version 2
Slice Method (Array)
Returns a section of an array.
Arrayobj.slice (start, [end])
Parameters
Arrayobj required option. An Array object.
Start Required option. The starting element of the part specified in Arrayobj is the zero-based subscript.
End option. The end element of the part specified in Arrayobj is the zero-based subscript.
Description
The slice method returns an Array object that contains the specified portion of the arrayobj.
The slice method is copied to the element specified by end, but does not include the element. If start is negative, it is the length + start processing, where length is the size of the array. If end is negative, it is treated as length + end where length is the length of the array. If End is omitted, the slice method is copied to the ending of arrayobj. If End appears before start, no elements are copied into the new array.
Example
In the following example, all elements in the myarray are copied to the NewArray except for the last element:
NewArray = Myarray.slice (0,-1)
Splice Method
Removes one or more elements from an array and, if necessary, inserts a new element at the position of the removed element, returning the removed element.
Arrayobj.splice (Start, DeleteCount, [item1[, item2[, ...) [, Itemn]]]
Parameters
Arrayobj required option. An Array object.
Start Required option. Specifies that the starting position of the element is removed from the array, which is calculated starting at 0.
DeleteCount required option. The number of elements to remove.
Item1, item2,.. ., itemn required option. The new element to insert at the location of the removed element.
Description
The splice method modifies arrayobj by removing the specified number of elements starting from the start position and inserting new elements. The return value is a new Array object that consists of the removed element.
Requirements
Version 5.5
Reverse Method
Returns an Array object in which the order of elements is reversed.
Arrayobj.reverse ()
Parameters
Arrayobj the required option, which is an Array object.
Description
The reverse method reverses the position of an element in an Array object. During execution, this method does not create a new Array object.
If the array is not contiguous, the reverse method creates an element in the array to populate the interval in the array. The value of all the elements created by this is undefined.
Example
The following example illustrates the use of the reverse method:
Copy Code code as follows:

function Reversedemo () {
var a, l; Declare a variable.
A = new Array (0,1,2,3,4); Creates an array and assigns a value.
L = A.reverse (); Reverses the contents of an array.
return (L); Returns an array of results.
}
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.