Array.prototype.splice()
The ‘JavaScript Array splice()’ method in javascript is used to add or remove elements from an array splice. It can be used to add new elements to an array splice at a specific index or to remove a range of elements from an array.
It also modifies the original array and returns the removed elements (if any) as an array.
Syntax
Syntax for splice javascript array:
array splice (start, delete Count, item1, item2, …);
Parameters for JavaScript Array splice()
array splice javascript parameters:
start |
the index at which to start making changes to the array |
delete Count |
the number of elements to remove |
‘item1, item2, …’ |
the elements to add to the array, starting at the index specified by the ‘start’ parameter |
Returned value:
The ‘splice JavaScript’ method returns an array containing deleted element.
Arguments for splice javascript array:
The first argument passed to the ‘splice()’ method is the index at which to start changing the array. If a negative number is passed, it counts from the end of the array.
The second argument is the number of elements to remove from the array. If zero is passed, no elements are removed.
The third argument and any additional arguments passed to the ‘splice()’ method are the elements to add to the array, starting from the index specified in the first argument.
Examples of JavaScript Array splice() method
If you have an array splice ‘[“a”, “b”, “c”, “d”]’ and you call ‘splice(2, 1, “new1”, “new2”)’, it will remove the element at index 2 ‘c’ and add the elements “new1” and “new2” to the array starting at index 2, resulting in ‘[“a”, “b”, “new1”, “new2”, “d”]’.
For example, the following code will remove the element at index 2 and add the elements “new1” and “new2” to the array starting at index 2:
let my Array = ["a", "b", "c", "d"];
let removed = myArray.splice(2, 1, "new1", "new2");
console.log(myArray); // ["a", "b", "new1", "new2", "d"]
console.log(removed); // ["c"]
From the above example, of javascript splice, it can be noted that the JavaScript Array splice can be used to add elements without removing anything by passing 0 as the second parameter.
The ‘splice()’ method modifies the original array and also returns the removed elements as a JavaScript Array splice.
Here are some examples of string splice javascript for your practice
Example 1:
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let removedFruits = fruits.splice(2, 0, "Lemon", "Kiwi");
console.log(fruits);
//output ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]
console.log(removedFruits);
//output []
This example adds “Lemon” and “Kiwi” to the ‘fruits’ array starting at index 2, and does not remove any elements.
Example 2:
Removing elements from an array in JavaScript Array splice() Method :
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let removedFruits = fruits.splice(1, 2);
console.log(fruits);
//output ["Banana", "Mango"]
console.log(removedFruits);
//output ["Orange", "Apple"]
In this example, elements starting from index 1 and 2 are removed from the ‘fruits’ array. The removed elements are [“Orange”, “Apple”]
Example 3:
Replacing elements in an javascript splice:
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let removedFruits = fruits.splice(1, 2, "Lemon", "Kiwi");
console.log(fruits);
//output ["Banana", "Lemon", "Kiwi", "Mango"]
console.log(removedFruits);
//output ["Orange", "Apple"]
In this example, elements starting from index 1 and 2 are removed from the ‘fruits’ array and replaced with “Lemon” and “Kiwi”. The removed elements are [“Orange”, “Apple”]
Conclusion:-
The ‘splice()’ method in splice javascript is a powerful array manipulation tool that allows you to add or remove elements from an array splice javascript.