Last Updated on April 14, 2024 by Roshan Parihar
To remove specific element from an array in Javascript, you can use the splice()
function with its argument as the index position of the specified array element.
The filter()
function can also be useful to remove the specified element of an array in javascript. Let’s find out with the different examples given below.
Remove Specific Element From Array Using indexOf()
and splice()
Functions in Javascript
It first finds the index position of the specified element using the indexOf()
function. If the index position is positive, it means the element is matching with the specified element.
After that, use the array.splice()
function to delete the matching element from the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<script> function RemSpecArrElem(){ var myArray= [9, 13, 21, 7, 3, 17]; var item = 7; var ArrIndex = myArray.indexOf(item); if(ArrIndex > -1){ myArray.splice(ArrIndex, 1); } document.getElementById("myNewArray").innerHTML = "<strong>The new array is: </strong>"+myArray; } </script> <button onclick="RemSpecArrElem()">Remove Specific Element</button> <p id="myNewArray"></p> |
Output
The specified element is 7 to remove from the array. To remove it, you have to click the button given above. It gives you output that is an array without the specified array element to remove.
Delete Specified Array Element Using filter()
Function in Javascript
If you want to remove the specified element from an array, use only the filter()
function. It requires only to return
the specified element to delete from the array. See the example given below to learn the method.
1 2 3 4 5 6 7 8 9 10 11 12 |
<script> function DelSpecArrElem(){ var myArray= [9, 13, 21, 7, 3, 17]; var myItem = 21; myArray = myArray.filter(function(item) { return item !== myItem; }); document.getElementById("myNewArray").innerHTML = "<strong>The new array is: </strong>"+myArray; } </script> <button onclick="DelSpecArrElem()">Delete Specific Element</button> <p id="myNewArray"></p> |
Output
The specified element is 21 that you have to remove from the given array. The above example contains the button and you can click it to delete the specified element from the array.
Using Javascript For Loop with If
Condition and splice()
In addition to the above methods, you can also use the combination of for
loop and if
condition to remove array elements in javascript. Firstly, find the length of the array to perform iteration over the given array according to the size.
The iteration is required to check if each element if matching with the specified element in each loop. When the specified element matching with the array element, use the splice()
and pass the index position to remove the element from the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<script> function RemSpecArrItem(){ var myArray= [9, 13, 21, 7, 3, 17]; var Item = 13; for(i = 0;i < myArray.length; i++){ if(myArray[i] == Item){ myArray.splice(i, 1); } } document.getElementById("myNewArray").innerHTML = "<strong>The new array is: </strong>"+myArray; } </script> <button onclick="RemSpecArrItem()">Remove Specific Array Item</button> <p id="myNewArray"></p> |
Output
The given element is 13 to remove from the array using javascript. Now, click the button given above to delete the specified element from the given array. After you click the button, you will get an output that shows the item is removed from the array.
You May Also Like to Read