Array objects in JavaScript come with some methods, and you can use these methods to operate on arrays.
join()
You can use the join() method to merge members in an array into a string:
The code copy 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("-"));//---------
From the example above, we can see that if the join() method does not have parameters, then JavaScript will use commas as delimiters to merge all members into a string; if the join() method accepts parameters, then this parameter will be used as delimiters.
reverse()
You can use the reverse() method to reverse the order of members in an array:
The code copy is as follows:
//reverse()
o.reverse();
console.log(o);//[3,2,1]
You can see that after calling the reverse() statement, the array itself will change.
The return result of executing the reverse() statement is the changed array object.
sort()
The members in the array can be sorted using the sort() method (arranged alphabetically by default). Like the reverse() statement, the sort() statement will modify the array itself and return the modified array object:
The code copy 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 ab}));//[33, 44, 111]
As you can see, the sort() statement also accepts a function as a parameter to implement custom sorting.
concat()
The array can be spliced using the concat() method:
The code copy 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([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 only returns the result after splicing and will not make any modifications to the array itself.
slice()
You can use the slice() statement to get the sub-array in an array:
The code copy 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 just returns the result after the operation and will not make any modifications to the array itself. For the two parameters in the slice() statement, JavaScript follows the principle of "before included, afterwards not included": the array members specified by the first parameter will appear in the subarray, while the array members specified by the second parameter will not appear.
splice()
You can use the splice() statement to insert and knock out the array. Its first parameter specifies the position to be inserted or knocked out (position member), the second parameter specifies the number of knocked out members (detected from position member), and starting from the third parameter, all parameters are inserted into the array (inserted from position member before). The result returned by the splice() statement is an array composed of knocked-out array members. Unlike concat() and slice(), splice() will modify the array itself.
The code copy 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(1,0,2,3,[4,5]);
console.log(e);//[1, 2, 3, [4, 5], 4, 5, 6, 7]