TypeScript Array splice()
Syntax & Examples
Array.splice() method
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.
Syntax of Array.splice()
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
.
✐ Examples
1 Using splice() to remove elements from an array
In TypeScript, you can use the splice()
method to remove elements from an array, starting at a specified index.
For example,
- Define an array
arr
with some initial values. - Use the
splice()
method to remove 2 elements starting from index 1. - Store the removed elements in the variable
removedElements
. - Log
arr
andremovedElements
to the console using theconsole.log()
method.
TypeScript Program
const arr = [1, 2, 3, 4, 5];
const removedElements = arr.splice(1, 2);
console.log(arr);
console.log(removedElements);
Output
[1, 4, 5] [2, 3]
2 Using splice() to replace elements in an array
In TypeScript, you can use the splice()
method to replace elements in an array, starting at a specified index.
For example,
- Define an array
arr
with some initial values. - Use the
splice()
method to remove 1 element starting from index 2, and insert 'a' and 'b' in its place. - Store the removed element in the variable
removedElement
. - Log
arr
andremovedElement
to the console using theconsole.log()
method.
TypeScript Program
const arr = [1, 2, 3, 4, 5];
const removedElement = arr.splice(2, 1, 'a', 'b');
console.log(arr);
console.log(removedElement);
Output
[1, 2, 'a', 'b', 4, 5] [3]
3 Using splice() to add elements to an array without removing any
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,
- Define an array
arr
with some initial values. - Use the
splice()
method to insert 'a' and 'b' at index 3 without removing any elements. - Log
arr
to the console using theconsole.log()
method.
TypeScript Program
const arr = [1, 2, 3, 4, 5];
arr.splice(3, 0, 'a', 'b');
console.log(arr);
Output
[1, 2, 3, 'a', 'b', 4, 5]
Summary
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.