Array additions and deletions
(last in, first out) the most recently added item was first removed
push () can take any number of arguments, add them to the end of the array, and return the modified array length
pop () deletes the item at the end of the array, length decreases, and returns the removed item
(FIFO) The first item of an array is removed
Push ()
shift () moves the first item in the array and returns the item, length decreases
unshift () Adds any item to the beginning of the array and returns the length of the new array;
IE7 and earlier versions there is a bias in the implementation of JS, and its unshift () method always returns undefined instead of the new length of the array. IE8 returns the correct length value in non-compatibility mode----------The third edition of JavaScript advanced programming
Change
The reverse () method flips the order of the array, such as
Const VALUES = [10,5,1,3,4]
Console.log ("[10,5,1,3,4] Invert" +values.reverse ())//[10,5,1,3,4] Invert 4,3,1,5,10
Console.log ("[10,5,1,3,4] Ascending operation" +values.sort (function (A, b) {return-A-a))//[10,5,1,3,4] for ascending operation 1,3,4,5,10
Console.log ("[10,5,1,3,4] for descending operation" +values.sort (function (A, b) {return b-a}))//[10,5,1,3,4] for descending operation 10,5,4,3,1
Console.log (values)//[10, 5, 4, 3, 1]
When you use the sort () method, the ToString () method of each array item is called, and then the resulting string is compared (what is the curious string to compare?). I was curious at the time) to determine how to sort, even if each item in the array is a numeric value, thesort () method compares the string,
The concat () method creates a copy of the current array, adds the accepted parameters to the end of the copy, and finally returns the newly constructed array. If, in the absence of a parameter, it simply copies the current array and returns a copy, if one or more arrays of arguments are passed as parameters, the method adds each item in the array to the result array.
Slice () The method can return the selected element from an existing array, which accepts one or two parameters, both the position and the end position, and if there is a parameter, the method returns all items starting at the position specified by the parameter to the end of the array, if it is a two parameter, Returns the entry between the starting position and the end position (without ending position), such as Arr.slice (1,5), which returns the position (index) between 1-4. Note: This method does not affect the original array , such as
Const COLORS = ["Red", "blue", "yellow", "gray"];
Console.log (Colors.slice (1,3))//["Blue", "yellow"]
Console.log (colors)//["Red", "blue", "yellow", "gray"]
If one of the parameters of the slice () method is a negative number, use the array length plus the item to determine the corresponding position, as
Const COLORS = ["Red", "blue", "yellow", "gray"];
Console.log (Colors.slice (-2,-1))//equivalent to = = Colors.slice (2,3) ["Yellow"]
If the end position is less than the start position, an empty array is returned.
The Splice () method is the most powerful method in the array, and the main purpose is to insert items into the middle of the array, using the following three ways
1. Delete: You can delete any number of items, just specify 2 parameters, the position of the first item to be deleted, and the number of items to be deleted.
Const COLORS = ["Red", "blue", "yellow", "gray"];
Colors.splice (ON)
Console.log (colors)//(2) ["Red", "gray"]
2. Insert: You can insert any number of items to the specified location, just provide 3 parameters, starting position, items to be deleted, and items to insert, such as
Const COLORS = ["Red", "blue", "yellow", "gray"];
Colors.splice (1,0, "pink");
Console.log (colors)//(5) ["Red", "pink", "blue", "yellow", "gray"]
3. Replace: You can insert any number of items to the specified location, simply provide 3 parameters, starting position, items to be deleted, and items to insert, such as
Const COLORS = ["Red", "blue", "yellow", "gray"];
Colors.splice ("Pink");
Console.log (colors)//(3) ["Red", "pink", "gray"]
The splice () method always returns an array that contains the items that were deleted from the original array, and an empty array if no items were deleted.
Check
Both methods of IndexOf () and lastIndexOf () accept two parameters; the subparagraphs (optional) to find represents the index at which to find the starting point, where indexOf () looks backwards from the beginning of the array, and LASTINDEXOF () looks from the end of the array, Both of these methods return the position (index) of the item to find in the array, and if no return-1 is found, the congruent operator is used when comparing the first parameter (that is, the item to find) with each item in the array.
The method of the array of small white essays