Introduction to Array Operations in JavaScript and javascript Array Operations
Array objects in JavaScript come with some methods that can be used to operate arrays.
Join ()
You can use the join () method to merge the members in the array into a string:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe 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 ):
Copy codeThe 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.
Copy codeThe 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]