How to remove multiple elements from array in JavaScript ?

Given an array containing array elements and the task is to remove multiple elements from the array using JavaScript. The indexes of elements are given which need to be removed from the JavaScript array.

Approach 1:

  • Store the index of array elements into another array which need to be removed.
  • Start a loop and run it to the number of elements in the array.
  • Use splice() method to remove the element at a particular index.

Example 1: This example uses splice() method to remove multiple elements from array.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
  
<head
    <title
        How to remove multiple elements
        from array in JavaScript ?
    </title>
</head
  
<body style = "text-align:center;"
  
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1>
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;">
    </p>
      
    <button onclick = "GFG_Fun()">
        Click Here
    </button>
      
    <p id = "GFG_DOWN" style =
        "color:green; font-size: 20px; font-weight: bold;">
    </p>
      
    <script>
        var up = document.getElementById('GFG_UP'); 
        var down = document.getElementById('GFG_DOWN'); 
        var arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
          
        up.innerHTML = "Click on the button to remove "
            + "multiple elements.<br>Array = [" + arr + "]"; 
          
        function GFG_Fun() { 
            remove = [0, 2];
              
            for (var i = remove.length -1; i >= 0; i--)
                arr.splice(remove[i], 1);
              
            down.innerHTML = arr;
        
    </script
</body
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • Store the index of array elements into another array which need to be removed.
  • Use filter() method on the array of elements.
  • Use indexOf() method to select only those elements which are not present in the indexes array.

Example 2: This example uses filter() method and indexOf() method to remove multiple elements from array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
  
<head
    <title
        How to remove multiple elements
        from array in JavaScript ?
    </title>
</head
  
<body style = "text-align:center;"
  
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1>
      
    <p id = "GFG_UP" style
        "font-size: 15px; font-weight: bold;">
    </p>
      
    <button onclick = "GFG_Fun()">
        Click Here
    </button>
      
    <p id = "GFG_DOWN" style =
        "color:green; font-size: 20px; font-weight: bold;">
    </p>
      
    <script>
        var up = document.getElementById('GFG_UP'); 
        var down = document.getElementById('GFG_DOWN'); 
        var arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
          
        up.innerHTML = "Click on the button to remove multiple"
                + " elements.<br>Array = [" + arr + "]"; 
          
        function GFG_Fun() {
            var indexes = [0, 1];
              
            arr = arr.filter(function(value, index) {
                return indexes.indexOf(index) == -1;
            })
              
            down.innerHTML = arr;
        
    </script
</body
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.