This article summarizes the common methods of javascript arrays for you, the specific content is as follows
1. Join() method:
The Array.join() method converts all the elements in the array into strings and links them together, returning the last generated string. You can also specify optional strings to separate the elements of the array in the generated string. If no delimiter is specified, a comma is used by default. Examples are as follows:
var a=[1,2,3]; a.join();//=> "1,2,3" Because there is no delimiter specified, the default is a comma. a.join("+");//=> "1+2+3" specifies the delimiter as + a.join("-");//=> "1-2-3" specifies the delimiter as -The Array.join() method is an inverse operation of the String.split() method, which splits the string into several blocks to create an array.
2. Reverse() method:
The Array.reverse() method flashes 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 it to the original array, but the elements inside have been flashbacked and rearranged. Examples are as follows:
var a=[1,2,3]; a.reverse();// =>a=[3,2,1];
3. sort() method:
The Array.sort() 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. Examples are as follows:
var a=[4,3,1,2] a.sort();// =>[1,2,3,4] a.sort(function(a,b){return ab;});//=>[1,2,3,4] sort a.sort(function(a,b){return ba;});//=>[4,3,2,1] sort in ascending orderIt 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 Array.concat() 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]; a.concat(1,2);// =>[5,6,7,1,2]; a.concat([1,2]);// =>[5,6,7,1,2]; a.concat(3,[1,2]);// =>[5,6,7,3,1,2]; a.concat([1,[2,3]]);// =>[5,6,7,1,[2,3]];
5. Slice() method:
The Array.slice() 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. Examples are as follows:
var a=[5,6,7,3,1,2]; a.slice(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. a.slice(1,3)// =>[6,7] The second parameter is the end position of the array index, (excluding) index>=1&&index<3; a.slice(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) a.slice(-3,-2)// =>[3] Similarly, as above.
6. Splice() method:
The Array.splice() 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]; a.splice(2);// =>[7,3,1,2] a=[5,6];// Pass in a parameter to represent all elements after deletion from the index. a.splice(2,2);// =>[7,3] a=[5,6,1,2]; The second parameter represents the number of elements deleted. a.splice(2,2,'a','b','c'); //=>[7,3] a=[5,6,'a','b','c',1,2];
7. Push() and unshift() methods:
The Array.push() method is to add an element to the end of the array, which returns the length of the new array; the Array.unshift() 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]; a.push(4,5);// a=[1,2,3,4,5]; The return value is 5; a.unshift(4,5);// a=[4,5,1,2,3]; The return value is 5; ☆ passable parameters can be one or more.
8. Pop() and shift() methods:
The Array.pop() method is to delete the last element in the array, which returns the deleted element; the Array.shift() method is to delete the front element of the array, which returns the deleted element.
var a=[5,6,7]; a.pop();// a=[5,6]; return value is 7 a.shift();// a=[6,7]; return value 5
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.