Signup/Sign In

How to remove a specific item from an array in JavaScript

Answer: Using splice() function

The splice() method enables us to delete the element from the given array. We can perform several other operations using this function which are deleting existing elements, insert new elements, and replace elements within an array.

Using this method, we can delete and replace existing elements within the array.

Syntax

Array.splice(position,num);

To delete elements from an array, we have to pass two arguments within the splice function, the two arguments are:

  • The position argument specifies the position of the first item to delete
  • The num argument specifies the number of elements to delete.

Example: Using splice() method

In this example, we have used the splice() method to remove the specific item from the given array.

<!DOCTYPE html>
<html>
<head>
  <title>remove a specific item from an array</title>
</head>
<body>
  <script type="text/javascript">
    let scores = [1, 2, 3, 4, 5];
    let deletedScores = scores.splice(0, 2);
    console.log(scores); 
    console.log(deletedScores);
  </script>
</body>
</html>

Output


[3, 4, 5]
[1, 2]

Using delete operator

We can also remove the specific array element from the given array using the delete operator. Deleting an element using the delete operator does not affect the length of the array. It remains the same even after deleting an element.

When we delete an array element using the delete method, then the array element does not remove from the array, but it becomes undefined.

Example: Using delete operator

In the given example, we have used the delete operator to remove the specified element from an array.

<!DOCTYPE html>
<html>
<head>
  <title>Deleting specific element from an array</title>
</head>
<body>
  <script type="text/javascript">
  	var ar = [1, 2, 3, 4, 5];
    delete ar[4]; 
    console.log( ar ); 
  </script>
</body>
</html>

Output


[1, 2, 3, 4, empty]

Using filter() method

The filter() method consists of the single parameter that is a callback method.

The callback method is triggered when the filter method iterates through the array elements. It will pass three values to the callback method; these values are the current value, index, and array.

We have to specify the condition for the elements of an array, and each element has to pass the given condition. If the element passed the given condition, then that element will be added to the filtered array.

Example: Using filter() method

In the given example, we have used the filter() method to remove the elements of an array.

<!DOCTYPE html>
<html>
<head>
  <title>Deleting specific element from an array</title>
</head>
<body>
  <script type="text/javascript">
  	  var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var filtered = array.filter(function(value, index, arr){ 
        return value > 5;
    });
    console.log(filtered);
  </script>
</body>
</html>

Output


[6, 7, 8, 9]

Conclusion

In this lesson, we have discussed how to remove a specific item from an array in JavaScript. There are several methods offered by JavaScript to remove the specified element from the given array. Here we have used the three methods, firstly we have used the splice() method. With the help of this method, we can remove or replace the specified element. Then we have removed the element using the delete operator. This operator does not completely remove the element from the array; it just makes the element undefined. After that, we have used the filter() method. This method creates a filtered array, and each element has to pass the specified test. The elements that pass the test will be stored in the filtered array; otherwise, they will be removed from the array.



About the author: