This article summarizes the common methods of javascript arrays for you, the specific content is as follows
1. Join() method:
The () method converts all the elements in the array into strings and links them together, returning the last generated string. You can also specify that optional strings are separated into the generated string to separate the elements of the array. If no delimiter is specified, a comma is used by default. Examples are as follows:
var a=[1,2,3]; ();//=> "1,2,3" Because there is no delimiter specified, the default is a comma. ("+");//=> "1+2+3" specifies the delimiter as + ("-");//=> "1-2-3" specifies the delimiter as -
The () method is the inverse operation of the () method, which divides the string into several blocks to create an array.
2. Reverse() method:
() method flashbacks the elements in the array and returns the flashback array. It is a new array that does not produce flashbacks in the original array. Returns to the original array, but the elements inside have been flashbacked and rearranged. Examples are as follows:
var a=[1,2,3]; ();// =>a=[3,2,1];
3. sort() method:
The () method sorts the elements in the array and returns the sorted array. If the parameters are not passed, the default is sorted in alphabetical order. The case is as follows:
var a=[4,3,1,2] ();// =>[1,2,3,4] (function(a,b){return a-b;});//=>[1,2,3,4] Arrangement in descending order (function(a,b){return b-a;});//=>[4,3,2,1] Ascending order
It is very convenient to use anonymous functions here, because there is no need to name the function if it is only used once.
4、concat():
The () method creates and returns a new array. The elements in the new array contain the elements that call the array and the values of the parameters passed in concat(). The parameters passed in can be separate values or arrays. Concat() will not flatten the array of the array recursively. Examples are as follows:
var a=[5,6,7]; (1,2);// =>[5,6,7,1,2]; ([1,2]);// =>[5,6,7,1,2]; (3,[1,2]);// =>[5,6,7,3,1,2]; ([1,[2,3]]);// =>[5,6,7,1,[2,3]];
5. Slice() method:
The () method returns a fragment or subarray of the specified array. You can pass one or two parameters in it, and the parameters can be positive or negative. The case is as follows:
var a=[5,6,7,3,1,2]; (1)// =>[6,7,3,1,2] The numerical parameter refers to the index of the array. One parameter indicates the starting position. If the second parameter is not passed, the default is the number of elements in the array. (1,3)// =>[6,7] The second parameter is the end position of the array index, (not included) index>=1&&index<3; (1,-3)// =>[6,7] When there are negative numbers in the parameters, you can convert positive numbers, the method is -3+6 (number of elements in the array) (-3,-2)// =>[3] Similarly, as above.
6. Splice() method:
The () method is a common method to insert or delete in an array. It will modify the called array, splice() can pass in three parameters. The first parameter indicates where the element index begins, the second parameter indicates the total number of elements deleted, and the third parameter indicates the inserted element. The position of element insertion is the position where the element starts deletion. Examples are as follows:
var a=[5,6,7,3,1,2]; (2);// =>[7,3,1,2] a=[5,6];// Pass in a parameter to indicate all elements after deletion from the index. (2,2);// =>[7,3] a=[5,6,1,2]; The second parameter represents the number of elements deleted. (2,2,'a','b','c'); //=>[7,3] a=[5,6,'a','b','c',1,2];
7. Push() and unshift() methods:
The () method is to add an element to the end of the array, which returns the length of the new array; the () method is to add an element to the front of the array, which returns the length of the new array. Examples are as follows:
var a=[1,2,3]; (4,5);// a=[1,2,3,4,5]; Return value is 5; (4,5);// a=[4,5,1,2,3]; Return value is 5; ☆ passes can be one or more parameters.
8. Pop() and shift() methods:
The () method is to delete the last element in the array, which returns the deleted element; the () method is to delete the front element of the array, which returns the deleted element.
var a=[5,6,7]; ();// a=[5,6]; return value is 7();// a=[6,7]; return value 5
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.