Join ()
Converts all elements in an array into strings and joins them, returning the string,
var arr=[1,2,3];var str=arr.join ("#"); Str= "1#2#3";
If no delimiter is specified, the default is comma
var str2=arr.join (); "Str2=";
Reverse ()
Reverses the order of the elements on the original array and returns the array.
var arr=[1,2,3];arr.reverse (); arr=[3,2,1];
Sort ()
Sorts the array elements on the original array and returns the arrays
When no parameters are used, the elements in the array are sorted alphabetically, more precisely, in the order in which the characters are encoded, such as
var arrnum=[2,11,3];var arrstr=["BB", "AA", "CC"];arrnum.sort ();//arrnum=[11,2,3]arrstr.sort ();//arrstr=["AA", "BB" , "CC"];
When there are different types of elements in the array, such as numbers and strings, the elements of the array are arrays, of course, if an element in an array has several data types, such as functions, arrays, objects, strings ... It makes no sense to sort this array again.
var arr1=["", 11,3];var Arr2=[[3,2],[111,3,222],[222,333]];arr1.sort (),//arr1=[11, "a", 3];arr2.sort ();//arr2=[[ 111,3,222],[222,333],[3,2]];
Sort () can accept a comparison function as a parameter, such as:
var arrnum2=[3,5,2,1,4];arrnum2.sort (function (A, b) {return-A;}); /arrnum2=[1,2,3,4,5];arrnum2.sort (function (A, b) {return b-a;}); /arrnum2=[5,4,3,2,1];
Concat ()
This does not know how to describe, directly see the results of the meaning, he put back a new array, will not modify the original array
var arr=[1,2,3];var arr2=[4,5];var arr3=[6,7,[8,9]];var str= "Hello Wrold"; var num=10;arr.concat (ARR2);//Return [1,2,3,4,5 ]arr.concat (str);//Return to []arr.concat, "Hello World" (num);//Return to [1,2,3,10];arr.concat (ARR3);//return [1,2,3,6,7,[8,9]]
Slice ()
Returns a sub-array of the specified array, returning a new array
He accepts two parameters, the returned array contains the first parameter at the specified position and all the array elements that do not contain the position specified by the second argument, such as
var arr=[1,2,3,4,5];arr.slice (1,3);//return [2,3];
If only one parameter is specified, all array elements starting at the position specified by the first argument to the end of the array are returned, as
var arr=[1,2,3,4,5];arr.slice (2);//return [3,4,5];
If the argument is a negative number, it indicates a reciprocal position, such as 1 for the first-to-last element, and 3 for the third-to-last element, such as
var arr=[1,2,3,4,5];arr.slice (1,-1);//return [2,3,4];
Arr.slice ( -3,-1); return [3,4];
Arr.slice ( -6,-1); return [1,2,3,4];
If the first argument is larger than the second argument, or the value of the parameter is larger than the array length, the following is a go home array []
var arr=[1,2,3,4,5];console.log (Arr.slice (4,1)); Console.log (Arr.slice ( -1,-5)); Console.log (Arr.slice (8,10));
Splice ()
Splice () The common method of inserting or deleting an array element is to manipulate the original array and modify the original array, while the element index value of the original array increases or decreases accordingly; Returns an array of deleted elements, or an empty array if the number of deletions is zero []
The first parameter specifies the position of the element to be inserted or deleted in the array, the second parameter specifies the number of elements to delete from the specified position, and if the second argument is omitted, all elements from the starting point to the end of the array are deleted.
var arr=[1,2,3,4,5,6];var returnval=arr.splice (4);//returnval=[5,6]; Arr=[1,2,3,4];returnval=arr.splice (+);//returnval=[2,3]; Arr=[1,4];returnval=arr.splice (//RETURNVAL=[4); ARR=[1];
inserting elements
var Arr=[1,2,3,4,5,6];var returnval=arr.splice (1,0, ' A ', ' B ');//returnval=[]; Arr=[1, ' A ', ' B ', 2,3,4,5,6];returnval=arr.splice (1,2,7,[8,9]);//returnval=["A", "B"]; arr=[1,7,[8,9],2,3,4,5,6];
Push () vs. Pop ()
Push (): Adds one or more elements at the end of the array and returns the new length of the array
var arr=[];var returnval=arr.push ("2", 3);//arr=[1, "2", 3], returnval=2;
Pop (): Deletes an element at the tail of the array and returns the deleted element
var arr=[1,2,3];var returnval=arr.pop ();//arr=[1, 2], returnval=3;
Two methods both modify and replace the original array
Unshift () and Shift ()
The Unshift () and the Shift () method are similar to push () and pop (), except that he inserts or deletes an array header, and the index of an existing element is followed by a sparse array
var arr=[1,2];var returnval=arr.unshift (3);//arr=[3,1,2], returnval=3;
var arr=[1,2,3];var returnval=arr.shift ();//arr=[2,3], returnval=1;
When inserting multiple elements at once, there is a difference between the results obtained from an inserted element, such as:
var arr=[1,2];var returnval=arr.unshift (4);//arr=[4,1,2], Returnval=3;returnval=arr.unshift (5);//arr=[5,4,1,2], Returnval=4;returnval=arr.unshift (6);//arr=[6,5,4,1,3], Returnval=5;var arr2=[1,2];var returnVal2=arr2.unshift ( 4,5,6);//arr2=[4,5,6,1,2], returnval2=5;
ToString ()
Of course the array also has the ToString () method, which returns the string as the join () method without parameters
var str=[1, "A", 3].tostring (); Str= "1,a,3";