The JavaScript Array.prototype.toSpliced() method is a convenient way to create a new array by splicing elements from the original array without mutating it. Unlike the traditional splice() method, which alters the original array, toSpliced() returns a new array, leaving the original array unchanged.
Syntax
let newArr = array.toSpliced(start, deleteCount, item1, item2, /* …, */ itemN);
Parameters
Parameters | Descriptions |
---|---|
start | The index at which to start changing the array. If greater than the length of the array, the start will be set to the length of the array. If negative, it will begin that many elements from the end. |
deleteCount | The number of elements to remove from the array starting from the start index. If deleteCount is omitted, or if its value is greater than the number of elements from start to the end of the array, all elements from start to the end of the array will be deleted. If deleteCount is 0 or negative, no elements are removed. |
item1, item2, /* …, */ itemN (optional) | The elements to add to the array, starting from the start index. If no elements are specified, toSpliced() simply removes elements from the array. |
Return Value
Value | Description |
---|---|
Array | Returns a new array with the specified elements removed and/or added. |
Example 1: Removing Elements
This code demonstrates how to use the toSpliced() method to remove elements from an array.
let arr = [1, 2, 3, 4, 5]; let newArr = arr.toSpliced(1, 2); console.log("Original Array: ", arr); console.log("New Array: ", newArr);
Output:
Original Array: [1, 2, 3, 4, 5]
New Array: [1, 4, 5]
Example 2: Adding Elements
This code shows how to add elements to an array using the toSpliced() method.
let arr = [1, 2, 3]; let newArr = arr.toSpliced(1, 0, 'a', 'b'); console.log("Original Array: ", arr); console.log("New Array: ", newArr);
Output:
Original Array: [1, 2, 3]
New Array: [1, 'a', 'b', 2, 3]
Example 3: Replacing Elements
This code demonstrates how to replace elements in an array using the toSpliced() method.
let arr = [1, 2, 3, 4, 5]; let newArr = arr.toSpliced(1, 2, 'a', 'b'); console.log("Original Array: ", arr); console.log("New Array: ", newArr);
Output:
Original Array: [1, 2, 3, 4, 5]
New Array: [1, 'a', 'b', 4, 5]
Example 4: Using Negative Start Index
This code shows how to use a negative start index with the toSpliced() method.
let arr = [1, 2, 3, 4, 5]; let newArr = arr.toSpliced(-2, 1); console.log("Original Array: ", arr); console.log("New Array: ", newArr);
Output:
Original Array: [1, 2, 3, 4, 5]
New Array: [1, 2, 3, 5]
Example 5: No Deletion or Addition
This code demonstrates when deleteCount is set to 0, no elements are removed, and when no items are added, the array remains unchanged.
let arr = [1, 2, 3, 4, 5]; let newArr = arr.toSpliced(2, 0); console.log("Original Array: ", arr); console.log("New Array: ", newArr);
Output:
Original Array: [1, 2, 3, 4, 5]
New Array: [1, 2, 3, 4, 5]
Supported Browsers
Chrome | Edge | Firefox | Opera | Safari |
---|---|---|---|---|
110 | 110 | 115 | 96 | 16 |