How to remove multiple elements from array in JavaScript ?
Last Updated :
05 Jun, 2024
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:
- 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' ]
- 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' ]
- 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]);
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' ]
Please Login to comment...