The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. This method modifies the original array and returns the removed elements.
There are 2 variations for the syntax of Array.splice() method. They are:
splice(start: number, deleteCount?: number): T[]
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. If negative, it will begin that many elements from the end of the array. |
deleteCount | optional | The number of elements to remove from the array. If omitted, all elements from start to the end of the array are removed. |
This method removes elements from the array starting from the specified index, and returns an array of the removed elements. If deleteCount is not specified, all elements from start to the end of the array are removed.
Returns value of type Array
.
splice(start: number, deleteCount: number, ...items: T[]): T[]
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. If negative, it will begin that many elements from the end of the array. |
deleteCount | required | The number of elements to remove from the array. If omitted, all elements from start to the end of the array are removed. |
items | optional | The elements to add to the array, beginning at the start index. |
This method removes elements from the array starting from the specified index, and inserts new elements in their place. Returns an array of the removed elements.
Returns value of type Array
.
In TypeScript, you can use the splice()
method to remove elements from an array, starting at a specified index.
For example,
arr
with some initial values.splice()
method to remove 2 elements starting from index 1.removedElements
.arr
and removedElements
to the console using the console.log()
method.const arr = [1, 2, 3, 4, 5];
const removedElements = arr.splice(1, 2);
console.log(arr);
console.log(removedElements);
[1, 4, 5] [2, 3]
In TypeScript, you can use the splice()
method to replace elements in an array, starting at a specified index.
For example,
arr
with some initial values.splice()
method to remove 1 element starting from index 2, and insert 'a' and 'b' in its place.removedElement
.arr
and removedElement
to the console using the console.log()
method.const arr = [1, 2, 3, 4, 5];
const removedElement = arr.splice(2, 1, 'a', 'b');
console.log(arr);
console.log(removedElement);
[1, 2, 'a', 'b', 4, 5] [3]
In TypeScript, you can use the splice()
method to add new elements to an array, starting at a specified index, without removing any elements.
For example,
arr
with some initial values.splice()
method to insert 'a' and 'b' at index 3 without removing any elements.arr
to the console using the console.log()
method.const arr = [1, 2, 3, 4, 5];
arr.splice(3, 0, 'a', 'b');
console.log(arr);
[1, 2, 3, 'a', 'b', 4, 5]
In this TypeScript tutorial, we learned about splice() method of Array: the syntax and few working examples with output and detailed explanation for each example.