Summary of common methods in JS array

Source: Internet
Author: User

Stack method (LIFO)

Arrayobj.push () method

Arrayobj.pop () method

ArrayObj.push(): Adds a new element to the end of the array and returns the new length of the array.
ArrayObj.pop(): Removes the last element of the array from the array and returns the element. Returns undefined if the array is empty.

Queue Method (FIFO)

Arrayobj.shift ()

Arrayobj.unshift ()

ArrayObj.shift(): The method is used to remove the first element in the array and return the value of the first element.
If the array is empty, the shift () method does nothing and returns undefined. Note that the method does not create a new array, but instead modifies the original array directly. The method changes the length of the array.

ArrayObj.unshift(): This method adds its parameter order to the head of the array. Instead of creating a new array, it modifies the array directly. Returns the length of the new array.
unshift()Under IE6,IE7, the data has been added successfully, but the return value is undefined.

Conversion method

Join () method

join()method is used to put all the elements in an array into a string. The elements are delimited by the specified delimiter. Returns a string.

Reorder Methods

Reverse () method

Sort () method

reverse()Method reverses the order of the array items.

var values = [1, 2, 3, 4, 5];values.reverse (); alert (values);   // 5,4,3,2,1

The initial values and order of the arrays here are 1, 2, 3, 4, 5. When the reverse () method of the array is called, the order of its values becomes 5, 4, 3, 2, 1.

sort()Method arranges arrays in ascending order-that is, the smallest value is at the front and the largest value is the last. To implement sorting, the sort () method invokes the ToString () transformation method for each array item, and then compares the resulting string to determine how to sort. Even if each item in the array is an array, the sort () method compares the string as follows:

var values = [0, 1, 5, ten,];values.sort (); alert (values);   // 0,1,10,15,5

This sort of ordering is not the best scenario in many cases. So the sort () method can accept a comparison function as a parameter so that we specify that the value is in front of that value.

The comparison function accepts two parameters, returns a negative number if the first argument should precede the second, and returns 0 if the two arguments are equal, or returns a positive number if the first argument is located after the second one. Here is a simple comparison function:

function Compare (value1, value2) {   if (value1 < value2) {       return -1;    Else if (value1 > value2) {       return 1;    Else {       return 0;   }}

This comparison function can be used for most data types, as long as it is passed as a parameter to the sort () method, as shown in the following example:

var values = [0, 1, 2, 5, ten,];values.sort (Compare); alert (values  ) ; // 0,1,5,10,15

After the comparison function is passed to the sort () method, the values remain in the correct ascending order. Of course, you can also produce a descending sort result by comparing functions, as long as the value returned by the Compare function is exchanged:

 function   compare (value1, value2) { if  (value1 < value2) { return  1;  else  if  (value1 > value2) { return  -1;  else   { return  0; }}  var  values = [0, 1, 2, 5, ten, 15 // 15,10,5,1,0  

The reverse () and sort () methods return values that are sorted after the array.

For numeric types or for which the ValueOf method returns an object type of numeric type, you can use a simpler comparison function. This function simply minus the first value with the second value:

function Compare (value1, value2) {   return value2- value1;}

Because the comparison function affects the sorting result by returning a value that is less than 0, equal to zero, or greater than 0, the subtraction operation can handle all cases appropriately.

Operation method

Concat () method

Slice () method

Splice () method

The Concat () method is used to concatenate two or more arrays.
The method does not alter the existing array, but only returns a copy of the concatenated array.

<script type= "Text/javascript" >var a = [];d ocument.write (a.concat (4,5)); </script>

Result of output: 1,2,3,4,5

slice(start,end)方法
Start Required. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.
End is optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array.
Return value: Returns a new array containing elements from start to end (excluding the element) in the Arrayobject.

splice()method is used to insert, delete, or replace elements of an array
Syntax: Arrayobject.splice (index,howmany,element1,....., elementx)

Index required. Specifies where to add/remove elements from.
This parameter is the subscript that begins inserting and/or deleting an array element, which must be a number.

Howmany required. Specifies how many elements should be deleted. Must be a number, but it can be "0".
If this parameter is not specified, all elements from index start to the end of the original array are deleted.

Element1 is optional. Specifies the new element to be added to the array. Start the insertion from the subscript at index point.
Elementx is optional. You can add several elements to an array.

Return value: If the element is removed from Arrayobject, the array containing the deleted element is returned.
Description: The Splice () method removes 0 or more elements starting at index and replaces those that were deleted with one or more values declared in the argument list.

The following is a brief summary of the slice () and splice () methods

Slice (start, end); The slice () method returns all items starting at the specified position of the parameter to the end of the current array. If there are two parameters, the method returns the item between the dead and ending positions, but not the ending position.

var colors = ["Red", "green", "blue", "yellow", "purple"); var colors2 = colors.slice (1); var colors3 = Colors.slice (1,4//  green, blue, yellow, purple//  Green, Blue, yellow

Splice () has the function of deleting, inserting, replacing

Delete
Requires two parameters, the position of the first item to delete, and the number of items to delete.

var colors = ["Red", "green", "Blue"]; var removed = Colors.splice (0,1//  greeen, Blue//  Red

Insert
Requires three parameters: Start position, 0 (number of items to delete) and items to insert

var colors = ["Red", "green", "Blue"]; var removed = Colors.splice (1,0, "Yellow", "orange"//  ["Red", "yellow", "orange", "green" "," Blue "]//  return null

Replace
Requires three parameters: the starting position, the number of items to delete, and any number of items to insert.

var colors = ["Red", "green", "Blue"]; var removed = Colors.splice ("Yellow", "orange"); Console.log (colors);   // ["Red", "yellow", "orange", "Blue"] // ["Green"]

Summary of common methods in JS array

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.