Introduction to array operations in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces array operations in JavaScript. This article describes join (), reverse (), sort (), concat (), slice (), and splice () for example, if you need several functions, you can refer to the array objects in JavaScript and use these methods to operate arrays.

Join ()

You can use the join () method to merge the members in the array into a string:


The code is as follows:


Var o = [1, 2, 3];
Console. log (o. join (); // 1, 2, 3
Console. log (o. join (""); // 1 2 3
Var emptyArray = new Array (10 );
Console. log (emptyArray. join ("-"));//----------


As shown in the preceding example, if the join () method does not contain parameters, JavaScript combines all the members into a string using commas as the separator. if join () the method accepts the parameter, which is used as a separator.

Reverse ()

You can use the reverse () method to reverse the order of members in the array:


The code is as follows:


// Reverse ()
O. reverse ();
Console. log (o); // [3, 2, 1]


As you can see, after the reverse () statement is called, the array itself changes.

The returned result of executing the reverse () statement is the array object after the change.

Sort ()

You can use the sort () method to sort the members in the array (in alphabetical order by default ). Like the reverse () statement, the sort () statement modifies the array itself and returns the modified array object:


The code is as follows:


Var a = ["Phone", "Mobile", "Canon"];
A. sort ();
Console. log (a); // ["Canon", "Mobile", "Phone", undefined, undefined]
Var B = [33, 44, 111];
Console. log (B. sort (); // [111, 33, 44]
Console. log (B. sort (function (a, B) {return a-B}); // [33, 44,111]


As you can see, the sort () statement also accepts a function as a parameter to implement custom sorting.

Concat ()

You can use the concat () method to splice arrays:


The code is as follows:


Var c = [1, 2, 3];
Console. log (c. concat (4); // [1, 2, 3, 4]
Console. log (c. concat (5, 6); // [1, 2, 3, 5, 6]
Console. log (c. concat ([7, 8]); // [1, 2, 3, 7, 8]
Console. log (c. concat ([9, 10], [11, 12]); // [1, 2, 3, 9, 10, 11, 12]
Console. log (c. concat ([, []); // [1, 2, 3, 42, 43, [44, 45]
Console. log (c); // [1, 2, 3]


As you can see, unlike reverse () and sort (), the concat () statement only returns the spliced result and does not modify the array itself.

Slice ()

You can use the slice () statement to obtain the sub-array (sub-array) in the array ):

The code is as follows:


Var d = [1, 2, 3, 4, 5, 6];
Console. log (d. slice (0, 3); // [1, 2, 3]
Console. log (d. slice (3, 1); // []


Like concat (), the slice () statement only returns the result after the operation and does not modify the array itself. For the two parameters in the slice () statement, JavaScript follows the principle of "pre-included and not included": The array members specified by the first parameter will appear in the subarray, the array member specified by the second parameter does not appear.

Splice ()

You can use the splice () statement to insert or drop out an array. The first parameter specifies the insert or drop-out position (location member), the second parameter specifies the number of drop-out members (starting from the position member), starting from the third parameter, all parameters are inserted into the array (inserted before the position member ). The result returned by the splice () statement is an array composed of the knocked-out Array members. Unlike concat () and slice (), splice () will modify the array itself.


The code is as follows:


Var e = [1, 2, 3, 4, 5, 6, 7];
Console. log (e. splice (1, 2); // [2, 3]
Console. log (e); // [1, 4, 5, 6, 7]
Console. log (e. length); // 5
E. splice (, []);
Console. log (e); // [1, 2, 3, [4, 5], 4, 5, 6, 7]

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.