The Wayback Machine - https://web.archive.org/web/20240809045454/https://www.geeksforgeeks.org/how-to-remove-multiple-elements-from-array-in-javascript/
Open In App

How to remove multiple elements from array in JavaScript ?

Last Updated : 05 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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

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

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

JavaScript
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

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

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

JavaScript
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

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

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

JavaScript
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' ]

Approach 4: Using Array.prototype.splice() with spread operator

The splice() method can not only remove elements from an array but also add new elements in their place. We can leverage this along with the spread operator (…) to remove multiple elements efficiently.

JavaScript
let arr = ['Geeks', 'GFG', 'Gf', 'GeeksForGeeks'];

function removeElements(indexes) {
    for (let i = indexes.length -1; i >= 0; i--)
        arr.splice(indexes[i], 1);
    console.log(arr);
}

removeElements([0, 2]);

Output
[ 'GFG', 'Geeks' ]

Approach 5: Using map() method with filter() method

In this approach, we first create a new array with the same length as the original array and mark the elements to be removed with a special value (e.g., undefined). Then, we use the filter() method to remove these marked elements.

Steps:

  • Create a new array of the same length as the original array, marking elements to be removed with undefined.
  • Use the filter() method to create a new array that excludes the undefined values.

Example: In this approach, we pass the indexes of elements to be removed to the removeElements() function. Inside the function, we use the splice() method to remove elements from the array starting from index 0 (the first element) and removing a number of elements equal to the length of the indexes array. We replace the removed elements with the filtered array, which includes only the elements whose indexes are not present in the indexes array. Finally, we log the modified array to the console.

JavaScript
let arr = ['Geeks', 'GFG', 'Geeks', 'GeeksForGeeks'];

function GFG_Fun() {
    const indexes = [0, 2];

    // Mark elements to be removed with undefined
    const markedArr = arr.map((value, index) => indexes.includes(index) ? undefined : value);

    // Filter out the undefined values
    arr = markedArr.filter(value => value !== undefined);

    console.log(arr);
}
GFG_Fun();

Output
[ 'GFG', 'GeeksForGeeks' ]




Previous Article
Next Article

Similar Reads

How to create an array with multiple objects having multiple nested key-value pairs in JavaScript ?
In this article, we will learn to create an array with multiple objects having key-value pairs, we will use nesting which means defining another array in a pre-defined array with key-value pairs. Suppose we have several arrays of objects means containing various key-value pairs where keys are uniquely identified now we have to add these arrays to a
3 min read
How to Remove Multiple Objects from Nested Array of Objects in JavaScript ?
A nested array of objects is an array of arrays that contain multiple objects as their elements. Removing multiple objects from the nested array of objects in JavaSript can be accomplished in different ways as listed below: Table of Content Using filter() with some() methodUsing filter() with includes() methodUsing filter( ) with findIndex() method
6 min read
How to remove specific elements from the left of a given array of elements using JavaScript ?
In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() methodUsing shift MethodApproach: Using splice() meth
2 min read
Replace multiple strings with multiple other strings in JavaScript
In this article, we are given a Sentence having multiple strings. The task is to replace multiple strings with new strings simultaneously instead of doing it one by one, using JavaScript. Below are a few methods to understand: Table of Content Using JavaScript replace() method Using the JavaScript str.replaceAll() methodUsing Array.reduce() method:
2 min read
How to Remove Multiple Object from Nested Object Array ?
Removing multiple objects from a nested object array in JavaScript involves traversing the array and filtering out the objects that meet certain criteria, such as having specific properties or values. This can be achieved using various array manipulation methods available in JavaScript. Below are the approaches used to remove the multiple object fr
3 min read
Remove Least Elements to Convert Array to Increasing Sequence using JavaScript
Given an array of numbers, our task is to find the minimum number of elements that need to be removed from the array so that the remaining elements form an increasing sequence. Examples: Input: arr = [5, 100, 6, 7, 100, 9, 8];Output: No of elements removed =3, Array after removing elements =[ 5, 6 ,7,8]Input: arr = [1, 100, 60, 7, 17, 19, 87];Outpu
3 min read
Remove empty elements from an array in JavaScript
Many times there are empty elements in an array. In this article, we will see the methods to remove empty elements from the array. Below are the approaches used to Remove empty elements from an array in JavaScript: Table of Content Using array.filter() methodUsing array.reduce() methodUsing for loop Using JavaScript Array flat() MethodMethod 1: Usi
3 min read
How to Remove duplicate elements from array in JavaScript ?
In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing indexOf() MethodUsing a third-party libraryUsing MapBel
4 min read
JavaScript Program to Remove Duplicate Elements From a Sorted Array
Given a sorted array arr[] of size N, the task is to remove the duplicate elements from the array. Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Explanation: All the elements are 2, So only keep one instance of 2. Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5}These are the following approaches: Table of Co
3 min read
How to remove n elements from the end of a given array in JavaScript ?
In this article, we will learn how to remove the last n elements from the end of the given array in JavaScript. We can remove n elements from the end of a given array in the following ways: Table of Content Using splice() MethodUsing pop() MethodUsing filter() MethodUsing Array slice() methodUsing while loopUsing the length PropertyMethod 1: Using
4 min read
three90RightbarBannerImg