Last Updated on April 17, 2024 by Roshan Parihar
To remove multiple elements from an array in javascript, use the for
loop with splice()
function.
The multiple elements can be a small part of the array to remove from it. The filter()
function can also be useful to delete many items from an array.
Let’s find out with the examples given below.
Using splice() in Loop to Remove Multiple Element from Array in Javascript
If you want to remove multiple elements of an array in javascript, you can use the for
loop inside which gets the index position of each element. After that, you can use the splice()
function to remove the element according to its index position.
Examples 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<script> function RemMultiArr(){ var myArr= [9, 13, 21, 7, 3, 17]; var ItemstoRemove = [13, 7, 17]; for(i = 0; i < ItemstoRemove.length; i++){ var ArrIndex = myArr.indexOf(ItemstoRemove[i]); if(ArrIndex > -1){ myArr.splice(ArrIndex, 1); } } document.getElementById("myNewArray").innerHTML = "<strong>The new array is: </strong>"+myArr; } </script> <button onclick="RemMultiArr()">Remove Array Element</button> <p id="myNewArray"></p> |
Output
The multiple specified elements to remove from an array are [13, 7, 17]
. When you click the button given above, these elements get removed from the given array. You can check the output to see the result of the method.
Remove with For Loop with filter() Function of Javascript
It uses the loop to filter out the multiple elements from the array using the filter()
. In each loop, the specified array element gets removed.
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<script> function RemMultiArr(){ var myArr= [9, 13, 21, 7, 3, 17]; var ItemstoRemove = [21, 3]; for(i = 0; i < ItemstoRemove.length; i++){ myArr = myArr.filter(function(item) { return item !== ItemstoRemove[i]; }); } document.getElementById("myNewArray").innerHTML = "<strong>The new array is: </strong>"+myArr; } </script> <button onclick="RemMultiArr()">Remove Array Element</button> <p id="myNewArray"></p> |
Output
Two elements are [21, 3]
to delete from the given array using javascript. When you click the button above, it will give you the output of the new array without the elements [21, 3]
.
Delete With Javascript filter() and includes() Function
In addition to the above examples, you can also use the filter()
function with the includes()
function in javascript as given below. It does not require to use of a loop to perform the deletion of the multiple array element.
Examples 3
1 2 3 4 5 6 7 8 9 10 |
<script> function RemArrItem(){ var myArr= [9, 13, 21, 7, 3, 17]; var MultiItemtoRemove = [9, 7, 17]; myArr = myArr.filter(item => !MultiItemtoRemove.includes(item)); document.getElementById("myNewArray").innerHTML = "<strong>The new array is: </strong>"+myArr; } </script> <button onclick="RemArrItem()">Remove Multiple Array Element</button> <p id="myNewArray"></p> |
Output
The elements to remove from the given array are [9, 7, 17]. You can click the button given above to remove multiple array elements. The output contains the array of items with removed multiple items.
You May Also Like to Read