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
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
LinkedList toArray() Method in Java In Java, the toArray() method is used to convert a LinkedList into an Array. It returns the same LinkedList elements but in the form of an Array only.Example: Here, we use toArray() to convert a LinkedList into an Array of Integer.Java// Java Program to Demonstrate how to use toArray() method // to
4 min read
Reverse an Array in Java Reversing an Array is a common task in every programming language. In Java, there are multiple ways to reverse an array. We can reverse it manually or by using built-in Java methods. In this article, we will discuss different methods to reverse an array with examples.Let us first see the most common
4 min read
ArrayList clone() method in Java with Examples The Java.util.ArrayList.clone() method is used to create a shallow copy of the mentioned array list. It just creates a copy of the list. Syntax: ArrayList.clone() Parameters: This method does not take any parameters. Return Value: This function returns a copy of the instance of Linked list. Below pr
2 min read
Java Collection toArray() Method The toArray() method of Java Collection returns an array containing elements that are inside the calling collection. This article will discuss the toArray() method, its syntaxes, how it works, and some code examples. Syntax of toArray() MethodObject[] toArray();Return Type: The return type of the ab
3 min read
CopyOnWriteArraySet toArray() method in Java with Example toArray() The Java.util.concurrent.CopyOnWriteArraySet.toArray() method returns an array containing all the elements in the set in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the
3 min read
Reverse an ArrayList in Java using ListIterator Assuming you have gone through arraylist in java and know about arraylist. This post contains different examples for reversing an arraylist which are given below:1. By writing our own function(Using additional space): reverseArrayList() method in RevArrayList class contains logic for reversing an ar
6 min read