1. Adding and deleting arrays
push() and pop():
The functions of these two methods are the same as those of the stack-out and Stack-in methods previously learned. They operate on the original array, not on the generation of a new modified array. push() adds one or more elements to the end of the array and returns the new length of the array; pop() deletes the last element of the array, reduces the length of the array, and returns the deleted value.
unshift() and shift():
Similar to push() and pop(), except they add and delete at the head of the array. unshift() is to add one or more elements to the header of an array (move other elements that already exist to a higher index) and return the new length of the array; shift() is the first element of the array that is deleted and returned (the other elements of the array are moved down one position to fill the empty space in the array header). Note that inserting multiple elements at once with unshift () does not behave the same as inserting multiple elements with multiple calls to unshift (), as shown below:
var a=[]; a.unshift(1); a.unshift(2); alert(a.toString());//a=[2,1] a.unshift(3,4); alert(a.toString());//a=[3,4,2,1],Not as one of the previous inserts into the head delete a[0]; alert(a.toString());//a=[,4,2,1],Use delete Deleting an element simply sets the location element to undefined,Not removed from the array
As you can see here, deleting an array element with delete does not change the length property of the array, nor does it move down from a high index to fill in empty spaces. In this way, deleting elements with delete makes the array sparse.
splice():
This method is a general method for inserting or deleting elements in an array. It modifies the elements of the original array, rather than creating a new modified array. splice() has several parameters and functions as follows:
- The first parameter specifies where insertions and/or deletions start
- Second parameter: specifies the number of elements to delete
- Then any number of parameters: specify the element to insert
Therefore, depending on the number of parameters provided, the splice() method has multiple implementation capabilities:
- If only one parameter splice(a) is provided, splice() performs a delete operation, meaning that all elements from the start bit a specified by the parameter to the end of the array will be deleted;
- If two parameters splice(a,b) are provided, it is still a delete operation, meaning that B elements are deleted starting from the starting bit a specified by the first parameter.
- If more than two parameters are provided, splice() completes both deletion and insertion (if the second parameter is set to 0, only insertion is performed).
var a=[1,2,3,4,5,6,7,8]; var n1=a.splice(4);//n1=[5,6,7,8],a=[1,2,3,4] var n2=a.splice(1,2);//n2=[2,3],a=[1,4] var n3=a.splice(1,0,'a','b');//n3=[],a=[1,'a','b',4] var n4=a.splice(1,2,[1,2],3);//n4=['a','b'],a=[1,[1,2],3,4]
concat():
This method is used to stitch new elements after an array, and the elements to be added are passed as parameters to the concat() method, which does not modify the original array and returns the newly created stitched array. It is worth noting that if the parameter contains an array, the elements of the parameter array are stitched in instead of the entire array being stitched together as a whole, but it does not recursively flatten the array of the array (that is, if the array as a parameter is also nested inside, the nested array is stitched together as a whole).
var a=[1,2,3]; a.concat(4,5); //Return[1,2,3,4,5] a.concat([4,5]);//Return[1,2,3,4,5] a.concat([4,5],[6,[7,8]]);//Return[1,2,3,4,5,6,[7,8]],length=7
2. Array to String Output
join():
This method converts all elements of an array into strings and concatenates them together to return the last generated string. It can receive a string parameter to be used as a delimiter, or a comma is used by default if no delimiter is specified.
toString():
The string returned by calling the join() method without any arguments.
var a=[1,2,3]; a.join();// output"1,2,3" a.join(" "); //output"1 2 3" a.toString(); //output"1,2,3"
3. Array sorting
reverse():
This method reverses the order of elements in an array and returns an array in reverse order. It changes on the basis of the original array, not creating a new inverse array.
sort():
This method sorts the elements of the array in alphabetical order and returns the sorted array. Similarly, it changes from the original array. If you want to sort in another way, you can pass a comparison function to the sort() method.
var a=[33,4,1111,222]; a.sort(); //Return[1111,222,33,4],It's not what we think of as sorting by numeric size a.sort(function(a,b){ //Sort values from smallest to largest return a-b; }); a.sort(function(a,b){ //Sort values from large to small return b-a; });