I. array. Slice (START, end) method:
Slice () is used to take part of an array and use it to copy the array. If the end parameter is omitted, the split array contains all elements starting from start to the end of the array.
Use it to copy the array, just one line:
VaR newarray = oldarray. Slice (0 );
Other Instructions:
1. If start is negative, it is processed as Length + start. Here, length is the length of the array, such as A. Slice (-), which is equivalent to a. Slice ).
2. If end is negative, it is processed as Length + end. Here, length is the length of the array, such as A. Slice (0,-1 ).
3. If end is omitted, the slice method will always be copied to the end of the source array, for example, A. Slice (1 ). If end appears before start, no elements are copied to the new array, such as A. Slice ).
4. For strings, the slice (START, end) method is also available.
II,Array. Sort () method:
The default sort () is sorted by character encoding:
Code1:
VaR testarray = [3,324,534, 665,];
Testarray. Sort ();
Alert (testarray );
The output result is:134 3 324 5345 5654 6546 665
Sort by size:
VaR testarray = [3,324,534, 665,];
Testarray. Sort (function (a, B) {return a-B ;});
Alert (testarray );
Here, a comparison function is passed Sort , The logic of the comparison function is: if the difference between the two parameters is less 0 , Indicates A Must appear in B Otherwise B . The output result is:
The output result is:3 134 324 665 5345 5654 6546,
Supplement: Sorting multidimensional arrays:
var arr = [
[,],
[,],
, ],
[,],
];
alert ("by default, the first row \ n" + arr. sort (). join ("\ n")
alert ("column \ n" + arr. sort (function (left, right) {return left [2]> right [2]? 1:-1 }). join ("\ n")
alert ("Inverted \ n" + arr. sort (function (left, right) {return left [2]> right [2]? -1:1}). Join ("\ n")
// Description: Alert (ARR. Sort (function (left, right) {return left> right? }))
// Here, the sort method uses the return value 1 or-1 of the Parameter Function to determine whether to forward or reverse