javascript array Array (list) add/delete

javascript array Array (list) add/delete

unshift: add the parameter to the beginning of the original array, and return the length of the array
pop: delete the last item of the original array, and return the value of the deleted element; if the array is empty, return undefined
push: add the parameter to the end of the original array and return The length of the array
concat: return a new array, which is a
splice formed by adding parameters to the original array (start, deleteCount, val1, val2,...): delete the deleteCount item from the start position, and insert val1, val2 from this position ,...
reverse: reverse the array order
sort(orderfunction): sort the array according to the specified parameters
slice(start,end): return a new array consisting of items from the specified start index to the end index in the original array

detailed:

1. Array creation
var arrayObj = new Array(); //Create an array
var arrayObj = new Array([size]); //Create an array and specify the length, note that it is not the upper limit, it is the length
var arrayObj = new Array( [element0[, element1[, …[, elementN]]]]); Create an array and assign a value. It
should be noted that although the second method creates an array with a specified length, in fact the array is variable length in all cases , That is to say, even if the length is specified as 5, the element can still be stored outside the specified length. Note: The length will change accordingly.

2. Access to the elements of the array
var testGetArrValue=arrayObj[1]; //Get the element value of the array
arrayObj[1] = "This is the new value"; // Assign a new value to the array element

3. Adding array elements
arrayObj. push([item1 [item2 [… [itemN ]]]]);// Add one or more new elements to the end of the array and return the new length of the array
arrayObj.unshift([item1 [ item2 [… [itemN ]]]]);// Add one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned
arrayObj.splice(insertPos,0,[item1[, item2 [,… [,itemN]]]]);//Insert one or more new elements into the specified position of the array, the element at the inserted position will automatically move back and return "".

4. Deletion of array elements
arrayObj.pop(); //Remove the last element and return the value of the element
arrayObj.shift(); //Remove the first element and return the value of the element, the elements in the array are automatically moved forward
arrayObj .splice(deletePos,deleteCount); //Delete the specified number of deleteCount elements starting from the specified position deletePos, return the removed elements in array form

5. Array interception and merging
arrayObj.slice(start, [end]); //return a part of the array in the form of an array. Note that the element corresponding to end is not included. If end is omitted, all elements after start will be copied
arrayObj.concat ([item1[, item2[,… [,itemN]]]]); //Connect multiple arrays (can also be strings, or a mixture of arrays and strings) into an array, and return the new connected an array of
finishing: www.baidu.com

6. The copy of the array
arrayObj.slice(0); //returns the copy array of the array, note that it is a new array, not pointing to
arrayObj.concat(); //returns the copy array of the array, note that it is a new array, Not pointing

7. Sorting of the array elements
arrayObj.reverse(); //Reverse the elements (the first row to the last, the last row to the front), return the array address
arrayObj.sort(); //sort the array elements, return the array address

8.
Stringification of array elements arrayObj.join(separator); //Returns a string, which connects each element value of the array together, separated by a separator.
toLocaleString, toString, valueOf: can be regarded as a special usage of join, not commonly used

Guess you like

Origin blog.csdn.net/qq_37192571/article/details/108882796