Introduction to JavaScript Common array methods

Source: Internet
Author: User
Tags arrays constructor

Join () Method:
Method Array.join () converts all elements of an array to strings and then joins them. You can specify an optional string to separate the elements in the result string. If a delimited string is not specified, the element is separated by a comma by default.

The code is as follows Copy Code

var arr=[1,2,3]; Defines an array of three elements with the direct amount of the array

var result = Arr.join (); result = = ' 1,2,3 ' becomes a string

var result = Arr.join ("_"); result = = ' 1_2_3 '

into a string. The join () function, if you do not specify a parameter, is equivalent to join (', ') whose default delimiter is "comma."
Reverse () Method:
Method The Array.reverse () method arranges the order of the elements of an array backwards, and returns an array after the reverse sequence. This is the operation on the original array. This is not to create a new array that directly aligns all the elements in the original array in reverse order.

The code is as follows Copy Code
var arr= new Array (' laonb ', ' bolo ', ' jo2 ', ' simaopig ');

Defines an array of four elements with a constructor function

The code is as follows Copy Code
var result = Arr.reverse (); Result[0] = = ' Simaopig ' result[1] = = ' Jo2 ' result[2] = = ' Bolo ' result[3] = ' laonb '

Sort () Method:
The method Array.Sort () is an inverse sequence of the array elements on the original array, returning the sorted arrays. It can also specify an array of parameters, but this parameter is more interesting. It is a closure of a two element value, and if invoked without passing arguments to it, it will sort the array elements alphabetically (if necessary, the elements can be temporarily converted to strings for comparison operations):

The code is as follows Copy Code

var arr = new Array (' Xiaoxiaozi ', ' laonb ', ' Juejin '); Defines an array of three elements with a constructor function

var result = Arr.sort ();

Juejin laonb Xiaoxiaozi If an array contains undefined elements, the elements will be placed at the end of the array.
If you want to sort the array in another order, you must pass a comparison function as an argument to the sort () function. This function determines which of its two parameters is in the sorted array, which is in the front, and which is after. If the first argument should precede the second argument, the comparison function returns a number less than 0. If the first argument should appear after the second argument, the comparison function returns a number greater than 0. If two parameters are equal (for example, their order is equal), then the comparison function returns 0.

The code is as follows Copy Code

var arr = [2,4,5,1];

Arr.sort (); The return is 1,2,4,5

Arr.sort (
function (First_param,second_param) {return Second_param-first_param}
); Return > by sort 0, 0, or < 0 results for 5,4,2,1

Concat () Method:
Method Array.concat () can create and return an array that contains the elements of the original array that called concat (), followed by the parameters of Concat (). If some of these parameters are arrays, it is especially important that it is expanded. But this concat () does not recursively expand an array of elements. That is, it expands only the outermost array within the parameter. That's a mess, look at the example:

The code is as follows Copy Code
var arr = [1,2,3,4];
Arr.concat (5); The result is 1,2,3,4,5
Arr.concat ([5],[6]);/The result is 1,2,3,4,5,6
Arr.concat ([5,6],[7,8]);/The result is 1,2,3,4,5,6,7,8
Arr.concat (5,[6,[7,8]]);//result is 1,2,3,4,5,6,[7,8]

Slice () Method:
The method Array.slice () method returns a child array of the specified array. It has two parameters that specify the starting and ending positions of the child array to which to return the middle subset. If the end position (the second argument) defaults, it is always returned to the end of the array. If the parameter is negative, it is intercepted from the back. Remember that this function is to create a new array. Well.
is done on the original array.

The code is as follows Copy Code

var arr = [1,2,3,4,5,6,7,8];

Arr.slice (1,3); The array element starts at 0 and returns the first to the middle element of the third digit: [2,3]
Alert (Arr.join ()); [1,2,3,4,5,6,7,8]
Arr.slice ( -3,-1); From the last countdown to the last digit: [6,7]splice () Method:
Array.splice ()

method is to insert or delete a common method of an array element, directly altering the original array. Although the name and the previous method is very similar, but the effect is completely different, you must pay attention to. Oh.
Splice () can remove elements from an array, insert new elements into an array, or perform both, which sounds weird. The elements of an array that are located after the inserted or deleted elements are moved as necessary to allow continuity of the remaining elements in an array. The first parameter of splice () specifies the position of the element to be inserted or deleted in the array. The second parameter specifies the number of elements to remove from the array. If the second argument defaults, all elements from the start element to the end of the array are deleted. Splice () returns an array after the element has been deleted, or an empty array if no elements are deleted.
operate on the new ARR

The code is as follows Copy Code

var arr = [1,2,3,4,5,6,7,8];
Arr.splice (4); Returns the part of [5,6,7,8] that was deleted, and Arr became [1,2,3,4]
Alert (Arr.join ()); 1,2,3,4
Arr.splice (1,2); The return is [2,3],arr changed into [1,4]

Arr.splice (1,1); //

Returns a [4],arr] the first two parameters of [1]splice () specify the array elements that should be deleted. These two parameters can be followed by any number of additional parameters that specify the element to begin inserting at the location specified by the first argument

The code is as follows Copy Code

var arr = [1,2,3,4,5];

Arr.splice (2,0, ' A ', ' B '); back [],arr into [1,2, ' A ', ' B ', 3,4,5]

Arr.splice (2,2,[1,2],3); return [' A ', ' B '],arr] into [1,2,[1,2],3,3,4,5] call,

This is a really bad story.

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.