“This is the 22nd day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
The splice() method removes old values from the array and inserts new values into the array. When you use splice(), you get two arrays, the first for the excluded array and the second for the edited new array. Note that this method changes the original array.
grammar
The splice(start[, deleteCount[, item1[, item2[,…]]]]) method takes arguments from the start index to delete deleteCount elements and returns an array of deleted elements.
start
Start is a 0 starting index that specifies the starting index position for modification. If not defined, the default value of start is 0. If start is greater than the index range of the array, the contents are added from the end of the array.
In addition, start can use a negative index, which indicates the number of digits starting from the bottom of the array (counting from -1, which means that -n is the NTH element penultimate, which is equivalent to array.length-n).
deleteCount
The deleteCount argument is an optional integer that represents 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 of the array after start are deleted.
If deleteCount is 0 or negative, the element is not removed, in which case at least one new element should be added.
Const arrNumbers =,3,5,6,7 [1]; console.log(arrNumbers.splice(0, 3)); // [ 1, 3, 5 ] console.log(arrNumbers); // [6, 7]Copy the code
item1, item2, … optional
The element to be added to the array starts at the start position. If not specified, splice() deletes only array elements. The position of the added element is after the index is dropped and ends before the index is dropped.
const arrNumbers = [1, 3, 5, 6, 7]; console.log(arrNumbers.splice(0, 3, 8, 9)); // [ 1, 3, 5 ] console.log(arrNumbers); // [8, 9, 6, 7]Copy the code
1. Delete elements
To delete an element, specify the start argument and the number of elements to delete. DeleteCount returns an array of deleted elements:
const arrNumbers = [1, 3, 5, 6, 7]; console.log(arrNumbers.splice(0)); // [ 1, 3, 5, 6, 7 ] console.log(arrNumbers); / / []Copy the code
2. Insert elements into the array
You can do this by using the third argument item1, item2… Insert or add elements. You can add any number of elements. You can change the insertion position by changing the start and deleteCount values.
const ColorNames = (start, deleteCount, arr) => { arr.splice(start, deleteCount, "Pink", "Black", "Green"); return arr; }; // Console. log(ColorNames(2, 0, ["Golden", "Brown"])); / / [' Golden ', 'Brown', 'Pink', 'Black', 'Green'] / / array before the console. The log (ColorNames (0, 0, [" Golden ", "Brown"])); // [ 'Pink', 'Black', 'Green', 'Golden', 'Brown' ]Copy the code
3. Replace array elements
It is often necessary to replace one or more elements of an array. There is no explicitly named method replace in JavaScript, but you can implement it through the splice method. The splice method does not just replace one or more elements; in fact, it “replaces” elements by deleting and adding them somewhere in the array.
const ColorNames = (arr) => {
arr.splice(1, 3, "Pink", "Black", "Green");
return arr;
};
console.log(ColorNames(["Golden", "Brown", "Yellow", "Blue", "Red"])); // [ 'Golden', 'Pink', 'Black', 'Green', 'Red' ]
Copy the code
In the code above, “Brown”, “Yellow”, “Blue” are replaced by “Pink”, “Black”, “Green”.