JavaScript Array Operation Method

Source: Internet
Author: User
Tags javascript array
I was writing the content of a JavaScript regular expression today, but I am not sure about it. So I will introduce the JavaScript array method first today. Arrays have always been an important part of programming languages. JavaScript provides a powerful method for operating array elements for array objects. The following is a brief introduction. Join () method

I was writing the content of a JavaScript regular expression today, but I am not sure about it. So I will introduce the JavaScript array method first today. Arrays have always been an important part of programming languages. JavaScript provides a powerful method for operating array elements for array objects. The following is a brief introduction.

Join () method:

The method Array. join () can convert all elements of an Array into strings and then connect them.You can specify an optional string to separate the elements in the result string. If no separator string is specified, the elements are separated by commas by default.

[Cc lang = 'javascript ']
Var arr = [, 3]; // use an array to directly define an array of three elements

Var result = arr. join (); // result = '1, 2, 3 'is a string

Var result = arr. join ("_"); // result = '1 _ 2_3 'is changed to a string
[/Cc]

In fact, if the join () function does not specify a parameter, it is equivalent to join (','). The default Delimiter is comma ".

Reverse () method:

The Array. reverse () method sorts Array elements in reverse order and returns the reverse Array. This operation is performed on the original array.This does not create a new array, but directly sorts all elements in the original number group in reverse order.

[Cc lang = 'javascript ']
Var arr = new Array ('laonb', 'bolo', 'jo2', 'simaopig '); // use a constructor to define an Array of four elements

Var result = arr. reverse (); // result [0] = 'simaopig 'result [1] = 'jo2' result [2] = 'bolo' result [3] = 'laonb'

[/Cc]

Sort () method:

The Array. sort () method is used to reverse the Array elements on the original Array and return the sorted Array.You can also specify an ordered parameter. However, this parameter is quite interesting. Which is? A closure of two element values. If it is not passed to its parameter during the call, it sorts the array elements alphabetically (if necessary, you can convert an element to a string for comparison ):

[Cc lang = 'javascript ']
Var arr = new Array ('xiaoxiaozi', 'laonb', 'juejin'); // you can use a constructor to define an Array of three elements.

Var result = arr. sort (); // juejin laonb xiaoxiaozi
[/Cc]

If the array contains undefined elements, these elements are placed at the end of the array.

If you want to sort arrays in other order, you must pass a comparison function as a parameter of the sort () function. This function determines which of its two parameters is in the sorting array and which is in the rear. If the first parameter is located before the second parameter, the comparison function returns a number smaller than 0. If the first parameter appears after the second parameter, the comparison function returns a number greater than 0. If two parameters are equal (for example, their order is equal), the comparison function returns 0.

[Cc lang = 'javascript ']
Var arr = [2, 4, 5, 1];

Arr. sort (); // The returned values are 1, 2, 4, and 5.

Arr. sort (
Function (first_param, second_param) {returnsecond_param-first_param}
); // Return> 0, 0, or <0 results: 5, 4, 2, 1
[/Cc]

Concat () method:

The method Array. concat () can be created and an Array is returned. This Array contains the elements of the original Array that calls concat (), followed by the concat () parameter.If some of the parameters are arrays, they are expanded, which is especially important. However, this concat () does not recursively expand an array whose elements are arrays. That is to say, it only expands the outermost array in the parameter. It's messy. Let's look at the example:

[Cc lang = 'javascript ']
Var arr = [1, 2, 4];
Arr. concat (5); // The result is 1, 2, 3, 4, 5.
Arr. concat ([5], [6]); // The result is 1, 2, 4, 5, 6.
Arr. concat ([], []); // The result is ,.
Arr. concat (5, [6, []); // The result is, [].
[/Cc]

Slice () method:

The method Array. slice () method returns the child Array of the specified Array.It has two parameters, specifying the starting and ending positions of the Child array, and returns a subset of the two. If the end position (the second parameter) is default, the result is always returned until the end of the array. If the parameter is negative, it is truncated from the back to the front. Remember that this function is used to create a new array. Well.

[Cc lang = 'javascript ']
// All operations are performed on the original array
Var arr = [1, 2, 3, 4, 5, 6, 7, 8];

Arr. slice (); // If the array element starts from 0, it returns the intermediate element from the first to the third: [2, 3]
Alert (arr. join (); // [1, 2, 3, 4, 5, 6, 7, 8]
Arr. slice (-3,-1); // The number between the last and last bits: [6, 7]
[/Cc]

Splice () method:

The Array. splice () method is a common method for inserting or deleting Array elements and directly changing the original Array.Although its name is very similar to the previous method, its function is completely different. You must pay attention to it. Haha.

Splice () can delete elements from the array, insert new elements into the array, or execute these two operations at the same time. It sounds weird. The array elements located after the inserted or deleted elements move as necessary to maintain continuity with the remaining elements of the 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 be deleted from the array. If the second parameter defaults, all elements from the start element to the end of the array are deleted. Splice () returns an array after the element is deleted. If no element is deleted, an empty array is returned.

[Cc lang = 'javascript ']
// Operate on the new arr
Var arr = [1, 2, 3, 4, 5, 6, 7, 8];
Arr. splice (4); // The returned result is the deleted part of [,]. The arr is changed to [,].
Alert (arr. join (); // 1, 2, 3, 4
Arr. splice (); // The returned result is [2, 3], and the arr is changed to [].

Arr. splice (); // The returned result is [4], and the arr is changed to [1].
[/Cc]

The first two parameters of splice () Specify the array elements to be deleted. These two parameters can also have any number of additional parameters. They specify the elements to be inserted from the position specified by the first parameter.

[Cc lang = 'javascript ']
Var arr = [1, 2, 4, 5];

Arr. splice (, 'A', 'B'); // Returns []. arr is changed to [, 'A', 'B', 5].

Arr. splice (, [], 3); // return ['A', 'B']. arr is changed to [, [],].
[/Cc]

Call, this article is really unpleasant. Well... End in a hurry

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.