Implementation of javascript Array of list Adding and Deleting

  • 2021-10-13 06:34:28
  • OfStack

javascript Array Array (list) Add/Remove

unshift: Adds a parameter to the beginning of the original array and returns the length of the array pop: Delete the last item of the original array and return the value of the deleted element; Returns undefined if the array is empty push: Adds parameters to the end of the original array and returns the length of the array concat: Returns 1 new array formed by adding parameters to the original array splice (start, deleteCount, val1, val2, …): Delete deleteCount entry from start position and insert val1, val2, … reverse: Reverse Array Order sort (orderfunction): Sorts an array by specified parameters slice (start, end): Returns a new array of entries from the specified start subscript to the end subscript in the original array

Details:

1. Array creation


var arrayObj = new Array();   // Create 1 Array of numbers 
var arrayObj = new Array([size]);   // Create 1 Array and specify the length, note that it is not the upper limit, but the length 
var arrayObj = new Array([element0[, element1[,  … [, elementN]]]]); Create 1 Arrays and assign values 

It should be noted that although the length of the array created by the second method is specified, in fact, the array is variable in all cases, which means that even if the length is specified as 5, the element can still be stored outside the specified length. Note that the length will change accordingly.

2. Access to elements of an array


var testGetArrValue=arrayObj[1]; // Gets the element values of the array 
arrayObj[1]=  "This is a new value" ; // Assign a new value to an array element 

3. Adding Array Elements


arrayObj. push([item1 [item2 [ …  [itemN ]]]]);//  Will 1 One or more new elements are added to the end of the array and the new length of the array is returned 
arrayObj.unshift([item1 [item2 [ …  [itemN ]]]]);//  Will 1 When new elements are added to the array, the elements in the array automatically move backward and return the new length of the array 
arrayObj.splice(insertPos,0,[item1[, item2[,  …  [,itemN]]]]);// Will 1 Three or more new elements are inserted into the array at the specified position, and the elements at the inserted position are automatically moved backward, returning "". 

4. Deletion of array elements


arrayObj.pop(); // Remove the last 1 Elements and returns the value of that element 
arrayObj.shift(); // Remove the foremost 1 Elements and returns the value of that element, and the elements in the array are automatically moved forward 
arrayObj.splice(deletePos,deleteCount); // Delete from the specified location deletePos The specified quantity to start deleteCount Returns the removed elements as an array 

5. Interception and merging of arrays


arrayObj.slice(start, [end]); // Returns an array as an array 1 Part, note that it does not include  end  Corresponding element, if omitted  end  Will copy  start  All elements after 
arrayObj.concat([item1[, item2[,  …  [,itemN]]]]); // Join multiple arrays (which can also be strings, or a mixture of arrays and strings) as 1 Arrays, returning the new array connected 

6. Copy of Array


arrayObj.slice(0); // Returns a copy array of the array, note that 1 New array, not pointing to 
arrayObj.concat(); // Returns a copy array of the array, note that 1 New array, not pointing to 

7. Sorting of Array Elements


arrayObj.reverse(); // Invert the elements (first to last, last to first) and return the array address 
arrayObj.sort(); // Sort array elements and return array addresses 

8. String of array elements


arrayObj.join(separator); // Returns a string that sets each of the 1 Element values are connected to the 1 Start, use in the middle  separator  Separate. 

toLocaleString, toString, valueOf: It can be regarded as a special usage of join and is not commonly used


Related articles: