I’ve been working with JavaScript arrays recently, and I’ve come across a situation where I need to remove certain items from an array. Here’s an example of what I mean:
let myArray = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
Let’s say I need to remove the ‘banana’ and ‘date’ from this array. What would be the most efficient way to achieve this? I would ideally like the solution to be scalable, in case I need to remove more items in the future.
I know that I could use a loop to iterate through the array and splice the array when the item is found, like so:
for (let i = 0; i < myArray.length; i++) {
if (myArray[i] === 'banana' || myArray[i] === 'date') {
myArray.splice(i, 1);
i--;
}
}
However, I’m wondering if there’s a better, more JavaScript-esque way to accomplish this task. Also, I’ve heard that using splice within a loop can lead to issues due to the changing indices.
Any assistance you could provide would be greatly appreciated.
I am expecting better results.
2
Answers
Here’s a more efficient and concise way to remove specific items from a JavaScript array without using splice:
In this solution, we use the filter method along with a Set to efficiently remove specific items from the array. The Set data structure allows for faster lookup, making the solution scalable and efficient.
Here’s how it works:
Create a Set named itemsToRemove containing the items you want to remove (‘banana’ and ‘date’ in this case).
Use the filter method on the myArray to create a new array that only includes items that are not present in the itemsToRemove set.
The result will be myArray with ‘banana’ and ‘date’ removed:
Using this approach, you can efficiently remove any number of specific items from the array without having to deal with changing indices and with better performance compared to using a splice inside a loop.
I would not worry about performance and just go for
Array.prototype.filter()
as:Anyway, here’s a playground with different option for you to take a look and experiment: