Splice and slice have been confused for a long time.
Source slice MDN
splice MDN
English class
We need to slice it
Splice: connection
Array.prototype.slice()
The slice() method returns a new array object that is a shallow copy (including begin, but not end) of the array determined by begin and end. The original array will not be changed.
const animals = ['ant'.'bison'.'camel'.'duck'.'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2.4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1.5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2));
// expected output: Array ["duck", "elephant"]
console.log(animals.slice(2, -1));
// expected output: Array ["camel", "duck"]
Copy the code
grammar
arr.slice([begin[, end]])
Copy the code
parameter
- Begin (Optional)
Extracts the index at the beginning (starting at 0) from which the original array elements are extracted.
If this parameter is negative, it extracts from the penultimate element of the original array, and slice(-2) extracts from the penultimate element of the original array to the last element, including the last element.
If begin is omitted, slice starts at index 0.
If BEGIN exceeds the index range of the original array, an empty array is returned.
- End (optional)
Extract the index at the end (starting at 0) and end the extraction of the original array element at that index.
Slice extracts all elements indexed from begin to end (including begin, but not end).
Slice (1,4) extracts all elements (elements indexed 1, 2, and 3) from the second element through the fourth element in the array.
If this parameter is negative, it indicates the penultimate element in the original array at the end of the extraction. Slice (-2,-1) extracts the penultimate element of the array to the last element (excluding the last element, i.e. only the penultimate element).
If end is omitted, slice extracts all the way to the end of the original array.
If end is greater than the length of the array, Slice will extract all the way to the end of the array.
The return value
- A new array containing the extracted elements.
Array.prototype.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.
const months = ['Jan'.'March'.'April'.'June'];
months.splice(1.0.'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4.1.'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]
Copy the code
grammar
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
Copy the code
parameter
- start
Specifies the starting position of the modification (counting from 0).
If the length of the array is exceeded, the contents are appended from the end of the array.
If it is negative, it represents the number of bits from the end of the array (counting from -1, which means -n is the NTH element to the last and equivalent to array.length-n); If the absolute value of a negative number is greater than the length of the array, the starting position is bit 0.
- DeleteCount (optional)
An integer representing the number of array elements to remove.
If deleteCount is greater than the total number of elements after start, all elements after start are deleted (including the start bit).
If deleteCount is omitted, or its value is greater than or equal to array.length-start (that is, if it is greater than or equal to the number of all elements after start), then all elements in the array after start are deleted.
If deleteCount is 0 or negative, the element is not removed. In this case, at least one new element should be added.
- item1, item2, … (optional)
The element to be added to the array starts at the start position. If not specified, splice() will delete only array elements.
The return value
An array of deleted elements. If only one element is removed, an array containing only one element is returned. If no element is deleted, an empty array is returned.