JavaScript Array toSpliced() Method
Last Updated :
16 Apr, 2024
In JavaScript, Array.toSpliced()
method is used to create a new array with some elements removed and/or replaced at a given index, without modifying the original array.
The difference between the new toSpliced() method and the old splice() method is that the new method creates a new array, keeping the original array unchanged, while the old method alters the original array.
Syntax:
array.toSpliced(start, deleteCount, item1, item2, ..., itemN)
Parameters:
start
: zero-based index from which the array will start changing. deleteCount
: integer indicating the number of elements to remove from start
. item1, item2, ..., itemN
: The elements to add to the array, beginning from start
Return value:
The return value is a new array that consists of all elements before start
, item1
, item2
, …, itemN
, and all elements after start
+ deleteCount
.
Example 1: In the below example, the Array.toSpliced() method is used to copy a part of a given array without modifying it.
JavaScript
const numbers = [10, 20, 30, 40, 50];
const sliced = numbers.toSpliced(1, 3);
console.log("Old Array: ", numbers);
console.log("New Array: ", sliced);
Output:
Old Array: [10, 20, 30, 40, 50]
New Array: [10, 50]
Example 2: In the below code, the Array.toSpliced() method is used to insert a value at specified index in array.
JavaScript
const days = ["Mon", "Wed", "Thurs", "Fri"];
const day2 = days.toSpliced(1, 0, "Tue");
console.log("New Array", day2);
console.log("Old Array", days);
Output:
New Array: ['Mon', 'Tue', 'Wed', 'Thurs', 'Fri']
Old Array: ['Mon', 'Wed', 'Thurs', 'Fri']
Example 3: In below code, the Array.toSpliced() method is used to delete the two words starting from index 2.
JavaScript
const words =
["Apple", "Banana", "Cherry",
"Date", "Elderberry", "Fig"];
const result = words.toSpliced(2, 2);
console.log("New Array: ", result);
console.log("Old Array: ", words);
Output:
New Array: ['Apple', 'Banana', 'Elderberry', 'Fig']
Old Array: ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry', 'Fig']
Example 4: In the below code, the Array.toSpliced() method is used to replace the one at index 2 with two other items.
JavaScript
const items =
["Chair", "Table", "Desk", "Sofa"];
const items2 = items.
toSpliced(1, 1, "Bookshelf", "Lamp");
console.log("New Array: ", items2);
console.log("Old Array: ", items);
Output:
New Array: ['Chair', 'Bookshelf', 'Lamp', 'Desk', 'Sofa']
Old Array: ['Chair', 'Table', 'Desk', 'Sofa']
Supported Browsers
- Google Chrome
- Edge
- Internet Explorer
- Firefox
- Opera
- Safari
Similar Reads
JavaScript Array splice() Method The splice() method in JavaScript is a powerful and flexible tool for modifying arrays. It allows us to add, remove, or replace elements within an array. Itâs often used to update or manage array contents in various ways.Syntaxarray.splice(startIndex, deleteCount, item1, item2, ..., itemN);startInde
4 min read
JavaScript Array splice() Method The splice() method in JavaScript is a powerful and flexible tool for modifying arrays. It allows us to add, remove, or replace elements within an array. Itâs often used to update or manage array contents in various ways.Syntaxarray.splice(startIndex, deleteCount, item1, item2, ..., itemN);startInde
4 min read
TypeScript Array splice() Method The Array.splice() is an inbuilt TypeScript function that changes the content of an array by adding new elements while removing old ones. It takes an index, a count of elements to remove, and optional elements to add, returning the removed elements and modifying the original array.Syntaxarray.splice
3 min read
ArrayList toArray() method in Java with Examples The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a para
3 min read
CopyOnWriteArrayList toArray() method in Java The toArray() method of CopyOnWriteArrayList is used to return an array containing all the elements in CopyOnWriteArrayList in the correct order. Syntax: public Object[] toArray() or public <T> T[] toArray(T[] a) Parameters: This method either accepts no parameters or it takes an array T[] a a
2 min read
CopyOnWriteArrayList toArray() method in Java The toArray() method of CopyOnWriteArrayList is used to return an array containing all the elements in CopyOnWriteArrayList in the correct order. Syntax: public Object[] toArray() or public <T> T[] toArray(T[] a) Parameters: This method either accepts no parameters or it takes an array T[] a a
2 min read