How to remove multiple elements from array in JavaScript ?

10 Oct, 2019
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.
<!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>
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.
<!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>
Output:
Before clicking on the button:
After clicking on the button:
javascript-array
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
Roadmap to Learn JavaScript For Beginners
JavaScript | Promises
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?