Open In App

How to remove multiple elements from array in JavaScript ?

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

These are the following methods to remove multiple elements from an array in JavaScript:



Approach 1: Using splice() method

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






const arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
function GFG_Fun() {
    const remove = [0, 2];
 
    for (let i = remove.length - 1; i >= 0; i--)
        arr.splice(remove[i], 1);
 
    console.log(arr);
}
 
GFG_Fun();

Output
[ 'GFG', 'GeeksForGeeks' ]

Approach 2: Using filter() method and indexOf() method

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




let arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
 
function GFG_Fun() {
    const indexes = [0, 1];
 
    arr = arr.filter((value, index) => !indexes.includes(index));
 
    console.log(arr);
}
GFG_Fun();

Output
[ 'Geek', 'GeeksForGeeks' ]

Approach 3: Using reduce() and indexOf() method

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




let arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
 
function GFG_Fun() {
    const indexes = [0, 1];
 
    arr = arr.reduce((acc, value, index) => {
        if (!indexes.includes(index)) {
            acc.push(value);
        }
        return acc;
    }, []);
 
    console.log(arr);
}
GFG_Fun();

Output
[ 'Geek', 'GeeksForGeeks' ]

Article Tags :