Filter a Set in JavaScript
This post will discuss how to filter a set in JavaScript.
Filtering a set means creating a new set with elements of the original set, but containing only the elements that satisfy a certain condition. Some of the methods we can use to filter a set in JavaScript are:
1. Using Set.forEach() function
A simple solution is to iterate over each element of the set using forEach(), and delete the element from the set if it does not meet the condition using the delete() function. The forEach() function takes a callback function as an argument, which is executed for each element of the set. The delete() function takes a value as an argument, and removes it from the set if it exists. This modifies the original set in-place. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Create a new set using the Set constructor let mySet = new Set([1, "a", 2, "b", 3, "c"]); // Define a callback function that checks if the element is a number function isNumber(value) { return typeof value === "number"; } // Iterate over the set using the forEach function mySet.forEach(function(value) { // If the element is not a number, delete it from the set if (!isNumber(value)) { mySet.delete(value); } }); // The set is now filtered to contain only numbers console.log(mySet); // Set(3) { 1, 2, 3 } |
We can also use a for…of loop to iterate over the values of a set and delete the ones that we don’t want from the original set using the Set.delete() function. This function also modifies the original set in-place.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Create a new set using the Set constructor let mySet = new Set([1, 2, 3, 4, 5]); // Iterate over the set using the for…of loop for (let value of mySet) { // remove even values if (value % 2 === 0) { mySet.delete(value); } } // The set is now filtered to contain only odd numbers console.log(mySet); // Set(3) { 1, 3, 5 } |
2. Using Array.filter() function
Another alternative is to use the Array.filter() function, which takes a callback function as an argument and returns a new array with elements passing the implemented condition. We can use this function to create a new array with the required elements from the original set, and then pass the filtered array to the Set() constructor. This will work, but requires conversion between the set and the array. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Create a new set using the Set constructor let mySet = new Set([1, "a", 2, "b", 3, "c"]); // Convert the set into an array using the spread operator let myArray = [...mySet]; // Filter the array using the filter function let filteredArray = myArray.filter(Number); // Convert the filtered array back into a set using the Set constructor let filteredSet = new Set(filteredArray); // The set is now filtered to contain only numbers console.log(filteredSet); // Set(3) { 1, 2, 3 } |
3. Using Lodash or Underscore.js
A more convenient way to filter a set in JavaScript is using a third-party library. To filter a set using Lodash or Underscore.js, we can use their filter() function. This function iterates over elements of a collection and returns an array of all elements that pass a predicate function. We can then convert the filtered array back into a set using the Set() constructor. For instance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Import Lodash module const _ = require("lodash"); // or underscore // Create a new set using the Set constructor let mySet = new Set([1, "a", 2, "b", 3, "c"]); // Filter the set using Lodash's or Underscore's filter function let filteredArray = _.filter([...mySet], Number); // Convert the filtered array back into a set using the Set constructor let filteredSet = new Set(filteredArray); // The set is now filtered to contain only numbers console.log(filteredSet); // Set(3) { 1, 2, 3 } |
That’s all about filtering 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 :)