In this tutorial, we would love to share with you how to remove the elements/items from the javascript array. We will describe splice() methods of javaScript to removing/adding the elements/items in an array.
In this tutorial, we will take several examples of the JavaScript splice() method for better demonstration.
The javascript splice() method is used to add or remove elements/items from an array.
array.splice(index, howmany, item1, ....., itemX)
Now we will take several examples of splice() method:
Let’s take an example, Suppose we have one array that names arr, we want to remove the elements in it. So let’s see the example below:
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ];
arr.splice(1,2);
console.log( arr );
["one", "four", "five", "six", "seven", "eight", "nine", "ten"]
Let’s take new an example, Suppose we have one array that names arr.
If you want to add the elements in arr array. So let’s see the example below:
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ];
arr.splice(0,0,"zero");
console.log( arr );
["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
At position 0, add the new items.
In this example, we will demonstrate how to add and remove the array.
Suppose, you have an array that name month. In this array, you want to add/remove the new items/elements. We will demonstrate how to add or remove the item in it. let’s see below example:
var months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb'); // add at index 1
console.log(months);
months.splice(4, 1, 'May'); // replaces 1 element at index 4
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
In this example, we will explain how to remove all the elements after particular index from an array. Let’s see the example below:
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ];
arr.splice(3);
console.log( arr );
[ "one", "two", "three"];
In this example, we will demonstrate how to replace the value of an array. Let’s see the below example
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ];
arr.splice(5,1,6);
console.log( arr );
["one", "two", "three", "four", "five", 6, "seven", "eight", "nine", "ten"]
Using the javascript splice() method to remove Objects from Array in JavaScript. Let’s see the example below.
We have an array object, we want to remove the id=3 object inside the array. So use the below method:
var arr = [
{id:1,name:'hello'},
{id:2,name:'world'},
{id:3,name:'cool'},
{id:4,name:'javascript'},
{id:5,name:'jquery'}
];
var ind = arr.findIndex(function(element){
return element.id===3;
})
if(ind!==-1){
arr.splice(ind, 1)
}
console.log (arr);
Let’s take second of example remove the object from JSON array in javascript. Let’s see the example below
var arr = [
{id:1,name:'hello'},
{id:2,name:'world'},
{id:3,name:'cool'},
{id:4,name:'javascript'},
{id:5,name:'jquery'}
];
arr.splice(arr, 4);
console.log (arr);
In this tutorial, you have learned how to extract/remove, add elements from JavaScript arrays using the splice() method.
#javascript #webdev #array