In fact, the ones I usually use are push and pop, but I still write them down for later use.
shift : delete the first item of the original array and return the value of the deleted element; if the array is empty, it returns undefined
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.shift(); //a: [2,3,4,5] b:1
unshift : adds the argument to the beginning of the original array and returns the length of the array
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.unshift(-2,-1); //a: [-2,-1,1,2,3,4,5] b:7
Note: The test return value in IE6.0 is always undefined, and the test return value in FF2.0 is 7, so the return value of this method is unreliable. When you need to use the return value, you can use splice instead of this method. This article comes from www.45it.com
pop : Delete the last item of the original array and return the value of the deleted element; if the array is empty, it returns undefined
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.pop(); //a: [1,2,3,4] b:5
push : add the argument to the end of the original array and return the length of the array
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.push(6,7); //a: [1,2,3,4,5,6,7] b:7
concat : Returns a new array, which is composed of adding parameters to the original array.
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.concat(6,7); //a: [1,2,3,4,5] b: [1,2,3,4,5,6,7]
splice (start,deleteCount,val1,val2,...): DeleteCount item from the start position, and insert val1,val2,...
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4]
var b = a.splice(0,1); //Same shift
a.splice(0,0,-2,-1); var b = a.length; //Same unshift
var b = a.splice(a.length-1,1); //Same pop
a.splice(a.length,0,6,7); var b = a.length; //Same push
--------------------------------------------------------------------------------------------------------------------------------
splice details :
The splice function method is to remove one or more elements from an array, and if necessary, insert a new element at the removed element position to return the removed element.
arrayObj.splice( start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
Among them, arrayObj is required. An Array object.
start is a must. Specifies the starting position for removing elements from the array, which is calculated from 0.
deleteCount is a must. The number of elements to be removed.
item1, item2,. .,itemN is a must-have option. New element to be inserted at the location of the removed element.
The splice function method in JavaScript can remove the specified number of elements starting from the start position and insert new elements, thereby modifying arrayObj. The return value is a new Array object composed of the removed elements.
--------------------------------------------------------------------------------------------------------------------------------
reverse : Inverse the array
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.reverse(); //a: [5,4,3,2,1] b: [5,4,3,2,1]
sort (orderfunction): sort the array by specified parameters
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.sort(); //a:[1,2,3,4,5] b:[1,2,3,4,5]
slice (start,end): Returns a new array composed of items from the specified start subscript to the end subscript in the original array.
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.slice(2,5); //a: [1,2,3,4,5] b: [3,4,5]
join (separator): group the elements of the array into a string, with separator as the separator. If omitted, use commas as the separator by default.
The code copy is as follows:
var a = [1,2,3,4,5];
var b = a.join("|"); //a: [1,2,3,4,5] b: "1|2|3|4|5"
The above is all about this article, I hope you can like it.