Push (args) can press multiple elements at a time and return the updated array length.
var oldarr=[1,2,3];
Alert (Oldarr.push (4,[5,6])) –>5 (this only evaluates [5,6] as an element, returning the updated array length 5)
At this time oldarr–>[1,2,3,4,[5,6]]
The pop () function only pops the last element at the end and returns the element that pops up and returns undefined if it is called pop () for the number of empty groups.
Alert (Oldarr.pop ()) –>[5,6] (this pops up the last element [5,6] instead of 6)
At this time oldarr–>[1,2,3,4]
Unshift and Shift
Both of these methods are performed on the head of the array, while others are similar to push and pop, but the Unshift method in IE returns the undefined
var oldarr2=[1,2];
Oldarr2.unshift (3) –>undefined
At this time OLDARR2 is –>[3,1,2]
Oldarr2.shift () –>3
At this point oldArr2 is []
Next, take a look at the powerful splice, which can be used to add, delete, and manipulate elements of an array at random positions, which are also modified on the original array.
Start in Splice (Start,deletecnt,args) indicates the beginning of the subscript, deletecnt indicates the number of elements to be removed from the start of the subscript, including the element, and the delete operation returns the deleted element. Args is used to replace deleted elements (which can have multiple arguments), and start and deletecnt must be numbers, and if not the number is attempted, the conversion fails as the zero processing.
Splice must have at least one start element, otherwise no action will be made. DELETECNT does not exist to delete start and all subsequent elements (ie, take 0 do not delete). Start can be negative, indicating that the calculation begins at the right end of the array. deletecnt If a negative number is not deleted, it is not possible to delete negative elements.
var oldarr3=[1,2];
Oldarr3.splice () –> "" (Returns an empty string, without any action, after Operation oldarr3–>[1,2])
Oldarr3.splice ("") –>[1,2] ("" Attempt to convert to a number failed to return 0, so delete 1, 2, after Operation Oldarr3–>[], but IE is a bit disgusting, do not do any action)
Oldarr3.splice ("1a") –> ibid.
Odlarr3.splice (0,2) –>[1,2] ("Start with the elements of subscript 0, delete two elements so delete oldarr3–>[])
Oldarr3.splice (0,-1) –> "" (starting from 0 subscript Delete-1 elements, so equal to no action, after Operation oldarr3–>[1,2])
Oldarr3.splice (–>2) (from subscript 1 to delete 1 elements, that is, delete 2, so after deletion oldarr3–>[1])
Oldarr3.splice (1,4) –>2 (delete 4 elements starting from subscript 1, 1 starts with only 1 elements, so delete 2, so delete oldarr3–>[1])
Oldarr3.splice ( -1,0,3) –> "" (remove 0 elements starting from subscript-1 i.e. 2 elements, then add element 3, so after Operation oldarr3–>[1,3,2])
Oldarr3.splice ( -1,1,3) –>2 (starting from 1, 2 elements to delete 1 elements and then adding element 3, after operation is oldarr3–>[1,3])
The next step is to talk about Concat, which is used to concatenate two or more arrays, which do not change the original array but only return a new array. When the parameter is concatenated, the element in the array is concatenated.
var oldarr4=[1,2];
Oldarr4.concat (3,4) –>[1,2,3,4]
Oldarr4.concat (3,4,[5,6]) –>[1,2,3,4,5,6] (this side adds element 5 and element 6 in [5,6])
Oldarr4.concat (3,[4,[5,6]]) –>[1,2,3,4,[5,6] [the innermost element of this side [5,6] is used throughout to add, not disassemble)
Sorting methods in arrays
Sort (function) is the ordering of the original array, and no new arrays are generated. The default sort () is compared by converting elements in an array to strings without parameters.
var oldarr5=[3,1,5,7,17] Look at this general idea that the OLDARR5 sort Oldarr5.sort () will return [1,3,5,7,17] According to the number from small to large, but look at the results actually return is [ 1,17,3,5,7] are converted to strings because of comparisons. Then the string is compared one by one if the first character is the same then the second, or directly return the comparison results, because "17″<" 3″ so it is conceivable that the result of sequencing is not the result of the general impression.
The sort (function) method in addition to the default non-parameter can also be passed to the custom sorting method, so that the results of the order can be controlled by themselves, how to arrange the row, is not very cool Ah, hehe. General Custom Function comparison functions that contain two parameters representing the left and right elements to compare. A result is then returned in a certain way, if the return value is greater than 0 to interchange the left and right elements, and if the return value is less than 0 or equal to 0, the left and right elements are not exchanged. Now take a look at the example
Arrange the original array in numbers from large to small:
var oldarr5=[3,1,5,7,17]; Initial array
function Mysort (left,right) {
if (left<right) {
return 1;} Exchange two if the left element is less than the right element
else{
return-1;} If the left element is greater than or equal to the right element does not swap
}
Of course, the above method can be simplified to
Funaction Mysort (left,right) {return right-left;}
Sort by an even number in the first odd number
var oldarr6=[3,6,7,18];//initial Array
function MySort2 (left,right) {
if (left%2==0) return-1;//does not swap if the left element is an even number
if (right%2==0) return 1; Swap if the right element is an even number
return 0; Do not exchange
}
The last slice not much, just to intercept some elements in the original array, return a new array, the original array will not change, and its operation is similar to the slice string
var oldarr7=[1,2,3,4];
Oldarr7.slice (0) –>[1,2,3,4]
Oldarr7.slice (0,2) –>[1,2]
Oldarr7.slice (0,0) –>[]
Oldarr7.slice (0,-1) –>[1,2,3]
Oldarr7.slice ( -3,-1) –>[2,3]
Oldarr4.slice ( -1,-3) –[]
Operation of JS Array push,pop,shift,unshift