This is the 19th day of my participation in the August More Text Challenge

1. Add, delete, review and revise

1.1 push

The push() method adds one or more elements to the end of the array and returns the length of the new array.

arr.push(element1, ... , elementN)

  • @params: The element to be added to the array (any data type, more than one can be added at a time, separated by commas)
  • @return: Returns the length of the new element in the array
  • Whether to change the original arrayChange:
var arr = [1.2.3];
var res = arr.push(6.7.8);    
console.log(res);   // res = 6
console.log(arr);   / /,2,3,6,7,8 [1]
Copy the code

1.2 the unshift

The unshift() method adds one or more elements to the beginning of an array and returns the array’s new length. This method changes the length of the array.

arr.unshift(element1, ... , elementN)

  • @params: The element to be added to the array (any data type, more than one can be added at a time, separated by commas)
  • @return: Returns the length of the new element in the array
  • Whether to change the original arrayChange:
var arr = [1.2.3];
var res = arr.unshift(6.7.8);    

console.log(res);  // res = 6
console.log(arr);  / /,7,8,1,2,3 [6]
Copy the code

1.3 pop

The pop() method removes the last element from the array and returns the value of that element. This method changes the length of the array.

arr.pop()

  • @params: no
  • @return: returns the deleted element of the array
  • Whether to change the original arrayChange:
var arr = [1.2.3];
var res = arr.pop();    

console.log(res);  // res = 3
console.log(arr);  / / [1, 2]
Copy the code

1.4 the shift

The shift() method removes the first element from the array and returns the value of that element. This method changes the length of the array.

arr.shift()

  • @params:
  • @return:
  • Whether to change the original arrayChange:
var arr = [1.2.3];
var res = arr.shift();    

console.log(res);  // res = 1
console.log(arr);  / / [2, 3]
Copy the code

1.5 splice

The splice() method modifies an array by deleting or replacing existing elements or adding new ones in place, and returns the modified contents as an array. This method changes the original array.

array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

  • @params: Unlimited parameters, n,m,x… The first argument n is mandatory (the subscript of the array, starting with the NTH element), the second argument (optional) represents the number of elements to be deleted (or replaced, depending on whether the third argument has a value), and the third argument (optional) represents the elements to be added (or replaced)
  • @returnThe return value is an array of delete items
  • Whether to change the original arrayChange:
/ / delete
var arr = [1.2.3];
var res = arr.splice(1);  // Pass only the first argument, indicating that the element with subscript 1 will be deleted from the last element
console.log(res);  / / [2, 3]
console.log(arr);  / / [1]
// arr.splice(0) : Can empty the array and store the contents of the original array in the new array (similar to array cloning)
// arr.splice(arr.length-1) : Delete the last item
// arr.splice(0, 1) : delete the first item

/ / add
var arr = [1.2.3];
var res = arr.splice(1.0.8.9);  // The second argument is 0, which means not to delete, and the next argument means to insert into the array, starting at 1, and moving the previous element back
console.log(res);  / / []
console.log(arr);  // [1, 8, 9, 2, 3]

// change (replace)
var arr = [1.2.3];
var res = arr.splice(1.1.8.9);  // The second argument is 1, which replaces the element with subscript 1
console.log(res);  / / [2]
console.log(arr);  // [1, 8, 9, 3]
Copy the code