Remove one or more elements from a set in JavaScript
This post will discuss how to remove one or more elements from a set in JavaScript.
There are several methods to remove elements from a set in JavaScript, depending on whether we want to remove a specific element by its value, clear the entire set, or conditionally remove elements from it. Here are some of the common functions:
1. Removing a single element from the Set
The Set object has a built-in function called Set.delete() that can remove a specified value from the set, if it exists. The delete() function returns a boolean value indicating whether the value was successfully removed or not. This method works for any type of value, including objects, as long as they are passed by reference. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Create a set const mySet = new Set([1, 2, 3]); // Delete 2 from the set console.log(mySet.delete(2)); // true // Delete 4 from the set console.log(mySet.delete(4)); // false // Log the set console.log(mySet); // Set(2) { 1, 3 } |
2. Conditionally removing elements from the Set
If we need to conditionally remove elements from a set in JavaScript, we can use a loop, such as a for…of loop, to iterate over the elements of the set and check if they match a certain condition. If they do, we can call the delete() function on the set to remove them. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// create a set with five numbers let mySet = new Set([1, 2, 3, 4, 5]); // loop over the numbers in the set for (let num of mySet) { // check if the number is even if (num % 2 == 0) { // remove the even number from the set mySet.delete(num); } } // Log the set console.log(mySet); // Set(3) {1, 3, 5} |
Alternatively, we can use the filter() function of the Array object, which takes a callback function as an argument and returns a new array with the elements that pass the test implemented by the function. We can use this function to create a new array with the elements that we want to keep from the original set, and then pass the array to the Set constructor to create a new set. This method works for any type of value, but it requires an extra step of converting the set to an array and back.
|
1 2 3 4 5 6 7 |
// create a set with five numbers let mySet = new Set([1, 2, 3, 4, 5]); mySet = new Set([...mySet].filter(x => x % 2 !== 0)); // Log the set console.log(mySet); // Set(3) {1, 3, 5} |
3. Clearing the entire Set
We can use the clear() function of the Set object, which removes all the elements from the set, making it empty. This function does not take any arguments and does not return any value. The following code example demonstrates its usage:
|
1 2 3 4 5 6 7 8 |
// Create a set const mySet = new Set([1, 2, 3]); // Clear the set mySet.clear(); // Log the set console.log(mySet); // Set(0) {} |
That’s all about removing one or more elements from a set in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)