Array Operation Method of JavaScript Array

Source: Internet
Author: User
Tags javascript array

Push method
Add the new element to an array and return the new Length Value of the array.
Arrayobj. Push ([Item1 [item2 [... [itemn])
Parameters
Arrayobj is required. An array object.
Item, item2,... itemn is optional. The new element of the array.
Description
The push method adds new elements in the order they appear. If one of the parameters is an array, the array will be added to the array as a single element. To merge two or more elements in an array, use the Concat method.
Example <textarea id="runcode60572"></textarea>
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

Requirements
Release 5.5
Pop Method
Removes the last element from the array and returns the element.
Arrayobj. Pop ()
The required arrayobj reference is an array object.
Description
If this array is empty, undefined is returned.
Example
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

Requirements
Release 5.5
Shift Method
Removes the first element from the array and returns the element.
Arrayobj. Shift ()
Parameters
The required arrayobj reference is an array object.
Description
The shift method removes the first element from the array and returns the element.
Requirements
Release 5.5
Unshift Method
Insert the specified element into the starting position of the array and return the array.
Arrayobj. unshift ([Item1 [, item2 [,... [, itemn])
Parameters
Arrayobj is required. An array object.
Item1, item2,..., itemn is optional. The element inserted to the start part of the array.
Description
The unshift method inserts these elements into the beginning of an array, so these elements will appear in the array in order of the parameter sequence.
Requirements
Release 5.5
Concat method (array)
Returns a new array consisting of two or more arrays.
Array1.concat ([Item1 [, item2 [,... [, itemn])
Parameters
Array1 is required. The array object to be connected to all other arrays.
Item1,..., itemn is optional. To connect to another project at the end of array1.
Description
The Concat method returns an array object that contains the connection between array1 and any other projects provided.
Project to be added (Item1... Itemn) is added to the array from left to right. If an item is an array, add its content to the end of array1. If the project is not an array, add it as a single array element to the end of the array.
Copy elements from the source array to the result array as follows:
For the object parameters copied from the array being connected to the new array, the Copied object still points to the same object. Any change in the new array and source array will lead to another change.
Only the values of the values or strings connected to the new array are copied. Changing the value of a group does not affect the value of another array.
Example
The following example illustrates the usage of the Concat method when arrays are used: CopyCode The Code is as follows: function concatarraydemo (){
VaR a, B, c, d;
A = new array (1, 2, 3 );
B = "jscript ";
C = new array (42, "VBScript );
D = A. Concat (B, c );
// Returns the array [1, 2, 3, "jscript", 42, "VBScript"]
Return (d );
}

Requirements
Version 3
Join Method
Returns the string value, which contains all elements of the connected array. The elements are separated by the specified separator.
Arrayobj. Join (separator)
Parameters
Arrayobj is required. Array object.
Separator is required. Is a String object, which serves as the separator between array elements in the final string object. If this parameter is omitted, an array element is separated by a comma.
Description
If the element in the array is not defined or is null, process it as an empty string.
Example
The following example illustrates the use of the join method.Copy codeThe Code is 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 whose elements have been sorted.
Arrayobj. Sort (sortfunction)
Parameters
Arrayobj is required. Any array object.
Sortfunction is optional. Is the name of the function used to determine the order of elements. If this parameter is omitted, the elements are listed in ascending order of ASCII characters.
Description
The sort method sorts array objects appropriately. During execution, no new array objects are created.
If a function is provided for the sortfunction parameter, the function must return one of the following values:
(1) negative value. If the first parameter is smaller than the second parameter.
(2) Zero. If the two parameters are equal.
(3) positive value. If the first parameter is greater than the second parameter.
Example
[Ctrl + A select all Note: If you need to introduce external JS, You need to refresh it to execute]

Requirements
Version 2
Slice Method (array)
Returns an array.
Arrayobj. Slice (START, [end])
Parameters
Arrayobj is required. An array object.
Start is required. The starting element of the part specified in arrayobj is the subscript calculated from scratch.
End option. The ending element of the part specified in arrayobj is the subscript calculated from scratch.
Description
The Slice Method returns an array object, which contains the specified part of arrayobj.
The Slice Method is always copied to the end specified element, but this element is not included. If start is negative, it is processed as Length + start. Here, length is the length of the array. If end is negative, it is processed as Length + end. Here, length is the length of the array. If end is omitted, the slice method will always be copied to the end of arrayobj. If end appears before start, no elements are copied to the new array.
Example
In the following example, all elements in myarray are copied to newarray except the last element:
Newarray = myarray. Slice (0,-1)
Splice Method
Remove one or more elements from an array. If necessary, insert a new element to the position of the removed element to return the removed element.
Arrayobj. splice (START, deletecount, [Item1 [, item2 [,... [, itemn])
Parameters
Arrayobj is required. An array object.
Start is required. Specifies the start position of the element to be removed from the array. This position is calculated from 0.
Deletecount is required. The number of elements to be removed.
Item1, item2,..., itemn is required. The new element to be inserted at the position of the removed element.
Description
The splice method can remove the specified number of elements starting from the start position and insert new elements to modify arrayobj. The returned value is a new array object consisting of the removed elements.
Requirements
Release 5.5
Reverse Method
Returns an array object whose element order is reversed.
Arrayobj. Reverse ()
Parameters
Arrayobj is required. This parameter is an array object.
Description
The reverse method reverses the element positions in an array object. During execution, this method does not create a new array object.
If the array is not continuous, the reverse method creates elements in the array to fill in the interval in the array. In this way, the values of all created elements are undefined.
Example
The following example illustrates how to use the reverse method: Copy code The Code is as follows: function reversedemo (){
VaR A, L; // declare the variable.
A = new array (, 4); // create an array and assign values.
L = A. Reverse (); // reverse the array content.
Return (l); // an array of returned 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.