The toSpliced() method of the Array class in JavaScript returns a new array with some elements removed and/or replaced at a given index, without modifying the original array.
There are 5 variations for the syntax of Array.toSpliced() method. They are:
toSpliced(start)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
This method returns a new array with all elements from the start index to the end of the array removed.
Returns value of type Array
.
toSpliced(start, deleteCount)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
This method returns a new array with the specified number of elements removed from the start index.
Returns value of type Array
.
toSpliced(start, deleteCount, item1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
item1 | optional | The element to add to the array, beginning at the start index. |
This method returns a new array with the specified number of elements removed from the start index and the specified item added.
Returns value of type Array
.
toSpliced(start, deleteCount, item1, item2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
item1 | optional | The first element to add to the array, beginning at the start index. |
item2 | optional | The second element to add to the array, beginning at the start index. |
This method returns a new array with the specified number of elements removed from the start index and the specified items added.
Returns value of type Array
.
toSpliced(start, deleteCount, item1, item2, /* …, */ itemN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
start | required | The index at which to start changing the array. |
deleteCount | optional | The number of elements to remove from the start index. |
item1 | optional | The first element to add to the array, beginning at the start index. |
item2 | optional | The second element to add to the array, beginning at the start index. |
itemN | optional | Additional elements to add to the array, beginning at the start index. |
This method returns a new array with the specified number of elements removed from the start index and the specified items added.
Returns value of type Array
.
In JavaScript, we can use the toSpliced() method to remove elements from an array without modifying the original array.
For example,
const arr = [1, 2, 3, 4, 5];
const newArr = arr.toSpliced(1, 2);
console.log(newArr); // Output: [1, 4, 5]
console.log(arr); // Output: [1, 2, 3, 4, 5]
[1, 4, 5] [1, 2, 3, 4, 5]
We can use the toSpliced() method to add elements to an array without modifying the original array.
For example,
const arr = ['a', 'b', 'e'];
const newArr = arr.toSpliced(2, 0, 'c', 'd');
console.log(newArr); // Output: ['a', 'b', 'c', 'd', 'e']
console.log(arr); // Output: ['a', 'b', 'e']
['a', 'b', 'c', 'd', 'e'] ['a', 'b', 'e']
We can use the toSpliced() method to replace elements in an array without modifying the original array.
For example,
const arr = [1, 2, 3, 4, 5];
const newArr = arr.toSpliced(1, 2, 'a', 'b');
console.log(newArr); // Output: [1, 'a', 'b', 4, 5]
console.log(arr); // Output: [1, 2, 3, 4, 5]
[1, 'a', 'b', 4, 5] [1, 2, 3, 4, 5]
In this JavaScript tutorial, we learned about toSpliced() method of Array: the syntax and few working examples with output and detailed explanation for each example.