Array Method Complete (Part 1)
Note: The first time to write a blog is a little nervous, if there are errors, welcome to point out that if the coincidence is purely coincidental, this summary reference book JavaScript authoritative guide, interested partners can go to read it.
join() method
This method converts all the elements in the array into strings and stitches them together, and finally returns the generated strings. You can specify an optional string to separate the elements of the array, using "," by default.
Example:
var arr = [1,2,3]; arr.join(); ==> output'1,2,3' arr.join(" ") ==> output'1 2 3' arr.join("") ==> output'123' //Print arr again at this time console.log(arr); ==> output[1,2,3]
reverse() method
This method reverses the elements in the array and returns them to the reverse array.
Example:
var arr = [1,4,3]; arr.reverse(); ==> output[3,4,1] //Print arr again at this time console.log(arr); ==> output[3,4,1]
sort() method
The method is to sort the elements in the array and return the sorted results. The default is to sort them in alphabetical order (if it is necessary to translate them into strings for comparison). If the array contains undefined elements, they will be arranged at the end of the array. A function can be passed, which determines the order of its two parameters in the ordered array. If the return value of the function is less than 0, the first parameter is in front, and vice versa, the first parameter is in the back. Assuming that the two values are equal, the return value is 0.
Example:
var arr = ['abc','cfg','1',undefined] arr.sort(); ==> output["1", "abc", "cfg", undefined] var arr1 = [2,5,3,7,6]; //The return value is less than 0, arranged from small to large arr1.sort(function (a,b) {return a - b}); ==> output[2, 3, 5, 6, 7] //The return value is greater than 0, arranged from large to small arr1.sort(function (a,b) {return b - a}); ==>output[7, 6, 5, 3, 2] //At this point, output arr1 console.log(arr1); ==>output[7, 6, 5, 3, 2]
concat method
This method splices the original array and the incoming parameters into a new array and returns them without modifying the original array. If the incoming parameters are arrays, each item in the array will be spliced with the original array.
Example:
var arr = [1,3]; arr.concat(5,4,[6,7,[8,9]],{s:'1'}); ==> output[1,3,5,4,6,7,[8,9],{s:'1'}] //Output arr at this time console.log(arr); ==>output[1,3]
slice(start,end) method
This method returns a fragment or subarray of a specified array, which can pass two parameters at most. When only one parameter is passed, it means that the array containing the starting position is returned from the specified starting position until the end. If two bits are passed, it returns an array containing the starting position, but not the ending position, if it is passed. Negative number, which indicates the reciprocal end position. This method does not change the original array.
Example:
var arr = [1,2,3,4]; arr.slice(0,2); ==> output[1,2] arr.slice(1); ==> output[2,3,4] arr.slice(-1,-3); ==> output[] arr.slice(-3,-2); ==> output[2] arr.slice(1,-1); ==> output[2,3] //Output arr at this time console.log(arr) ==> output[1,2,3,4]
splice() method
This method can insert or delete elements in an array, in which parameters can be passed. The first parameter represents the starting position of insertion or deletion, the second parameter is the number of deleted elements specified. Without transmission, elements from the starting point to the end of the array will be deleted, and the parameters immediately following will be inserted into the array. Element, remember that its return value is an array of deleted elements, which modifies the original array
Example:
var arr = [1,2,3,4]; var a = arr.splice(2); ==> output a The value is[3,4],here arr The value is[1,2] //The upper and lower correspond to the results of different ways of operation. var a = arr.splice(1,2); ==>output a The value is[2,3],here arr The value is[1,4] var a = arr.splice(1,2,5,6); ==>output a The value is[2,3],here arr The value is[1,5,6,4] var a = arr.splice(1,2,[5,6]); ==>output a The value is[2,3],here arr The value is[1,[5,6],4] //This method is different from concat in that it inserts whatever it specifies and does not change.
push() method, pop() method
1. The push method adds one or more elements at the end of the array and returns the new length of the array.
Example:
var arr = [1,2]; var a = arr.push([],{},3,[1,2],{s:'haha'}); ==>output a The value is 6. //The original array arr is [1,2,[],{},[1,2],{s:'haha'}]
2. The pop method is to delete the last element of the array and return the deleted value.
Example:
var arr = [1,2,3]; var a = arr.pop(); ==>output a The value is 3, the original array arr The value is[1,2] //It's useless to pass parameters in pop, which returns the last bit of the array.
unshift() method and shift() method
1. The unshift () method inserts the specified element at the front of the array and returns the length of the new array.
var arr = [1,2,3];
var a = arr.unshift(4,5,6); ==>output a The value of 6, the original array arr by[4,5,6,1,2,3]
2. The shift () method deletes the first element of the array and returns the deleted value.
var arr = [1,2,3];
var a = arr.shift(); ==>output a The value of 1 is the original array. arr by[2,3]
//The shift method does not have much use for passing parameters. It returns the first place in the array.
toString() method, toLocaleString() method
1. The toString () method converts each element in its array into a string and, if necessary, calls the toString method of the element.
var arr = [1,2,3]; var a = arr.unshift(4,5,6); ==>output a The value of 6, the original array arr by[4,5,6,1,2,3]
var arr = [1,2,3]; var a = arr.shift(); ==>output a The value of 1 is the original array. arr by[2,3] //The shift method does not have much use for passing parameters. It returns the first place in the array.
toString() method, toLocaleString() method
1. The toString () method converts each element in its array into a string and, if necessary, calls the toString method of the element.
Note: Output does not include square brackets or any other form of package array worth separators
var arr = ['a',1,[2,3],[],{},{s:'hello'}];
var a = arr.toString();
//The value of output a is "a, 1, 2, 3, [object object object], [object object object]"
2. The toLocaleString () method is not introduced here. Details here
Summary:
The value methods that modify the original array are push(), pop(), unshift(), shift(), splice().
There are reverse() (inversion), sort() (sort) that modify the order of the original array.
var arr = ['a',1,[2,3],[],{},{s:'hello'}]; var a = arr.toString(); //The value of output a is "a, 1, 2, 3, [object object object], [object object object]"