The Practical Guide To Array Method: splice()
The Array.splice(str) method is a swiss army knife for arrays. It can do everything: add, remove and insert elements.
In this Article, I will tell the basic of splice() Method and how to use splice() Method in place of push(), pop(), shift(), unshift() and concat()
What is Splice ?
The splice() the method changes the contents of an array by removing or replacing existing elements and/or adding new elements.
Syntax
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])Start: Required. An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array.
-If start value is negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if the absolute value is greater than the length of the array.
deleteCount : Optional. The number of items to be removed. If set to 0, no items will be removed
item1, item2, … : Optional. The new item(s) to be added to the array
Get Harshaan Nihal Khan’s stories in your inbox
Join Medium for free to get updates from this writer.
RETURN VALUE: An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.
var months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at 1st index position
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']months.splice(4, 1, 'May');
// replaces 1 element at 4th index
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
Note:- splice() will mutate the Original Array.
splice()as apush()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
arr.push(...items)– adds items to the end of Array arr
let arr = ["I", "study", "JavaScript"];
arr.push("right","now");console.log(arr); // ["I", "study", "JavaScript","right","now"]
We can archive the same result by using splice as
let arr = ["I", "study", "JavaScript"];
arr.splice(arr.length, 0 ,"rigth","now");
console.log(arr); // ["I", "study", "JavaScript","right","now"]
splice()as apop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
arr.pop()– extracts an item from the end or Array arr
let arr = ["I", "study", "JavaScript"];
arr.pop();console.log(arr) // ["I", "study"]
We can archive the same result by using splice as
let arr = ["I", "study", "JavaScript"];
arr.splice(arr.length-1, 1);
console.log(arr) // ["I", "study"]//or using negative index
arr = ["I", "study", "JavaScript"];
arr.splice(-1,1);
console.log(arr) // ["I", "study"]
splice()as aunshift()
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
arr.unshift(...items)– adds items to the beginning.
let arr = [4,5,6];
arr.unshift(1,2,3);
console.log(arr); // [1, 2, 3, 4, 5, 6]We can archive the same result by using splice as
let arr = [4,5,6];
arr.splice(0,0,1,2,3);
console.log(arr); // [1, 2, 3, 4, 5, 6]
splice()as ashift()
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.
arr.shift()– extracts an item from the beginning,
var arr = [1, 2, 3];
arr.shift();
console.log(arr); // [2, 3]We can archive the same result by using splice as
var arr = [1, 2, 3];
arr.splice(0,1)
console.log(arr); // [2, 3]
splice()as aconcat()
The concat() method is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
var arr = array1.concat(array2)console.log(arr);
// expected output: Array ["a", "b", "c", "d", "e", "f"]//but both array1 & array2 will remain same
as I have mentioned concat () don't mutate the array but splice does so
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
var arr = array1.splice(array1.length,0,...array2)console.log(arr); // [] as splice only return omitted items
console.log(array1); // ["a", "b", "c", "d", "e", "f"]
console.log(array2): // ['d', 'e', 'f']






