The array objects in JavaScript come with methods that you can use to manipulate the arrays.
Join ()
You can use the join () method to merge members from an array into a string:
Copy Code code 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 you can see from the example above, if the join () method takes no arguments, JavaScript merges all members into a string using commas as a delimiter, and if the join () method accepts parameters, the argument is used as a delimiter.
Reverse ()
You can use the reverse () method to reverse the order of members in an array:
Copy Code code as follows:
Reverse ()
O.reverse ();
Console.log (o);//[3,2,1]
As you can see, the array itself will change after the reverse () statement is invoked.
The result of the execution of the reverse () statement is the changed array object.
Sort ()
You can use the sort () method to sort the members in an array (by default, in alphabetical order). Like the reverse () statement, the sort () statement modifies the group itself and returns the modified array object:
Copy Code code 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 a custom sort.
Concat ()
You can use the Concat () method to stitch the array:
Copy Code code 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 ([42,43,[44,45]));//[1, 2, 3, 42, 43, [44, 45]]
Console.log (c);//[1, 2, 3]
As you can see, unlike reverse () and sort (), the concat () statement simply returns the concatenation result, and the array itself does not produce any modifications.
Slice ()
You can use the slice () statement to get a child array (sub-array) in an array:
Copy Code code 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));//[]
As with Concat (), the slice () statement simply returns the result of the operation, and the array itself does not produce any modifications. For the two parameters in the slice () statement, JavaScript follows the principle of "not included before": the array member specified by the first argument appears in the child array, and the array member specified by the second argument does not appear.
Splice ()
You can use the splice () statement to insert and knockout arrays. The first parameter specifies the position of the insertion or knockout (the position member), the second parameter specifies the number of knockout members (starting from the position member), starting with the third argument, all parameters are inserted into the array (inserted before the position member). The result returned by the splice () statement is an array of the members of the knockout array. Unlike concat () and slice (), splice () makes changes to the array itself.
Copy Code code 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 (1,0,2,3,[4,5]);
Console.log (e);//[1, 2, 3, [4, 5], 4, 5, 6, 7]