Hello!
Hello there! This is my first post on dev.to. I Just want to share a quick guide on how to use sets in javascript.
Sets
Similar to mathematics, a set is a collection of different kind of elements, numbers, symbols, vectors, variables or even other sets. [1] To define a set in Javascript, all you need to do is call the Set constructor, for example: new Set([false, 1, "2", 3, [4, 5], {"6": 7}])
here, I defined a set with different values, you can also add other types of values, such as numbers, strings, arrays and objects.
Basic Use
These are the basic methods you can use with sets: add
, has
, size
, delete
and clear
.
const mySet = new Set()
mySet.add(0) // set(1) {0}
mySet.add(100) // set(2) {0, 100}
mySet.has(-1) // false
mySet.has(100) // true
mySet.size // 2
mySet.delete(100) // true
mySet // set(1) {0}
mySet.clear() // undefinded
mySet // set []
Applications
1. You can quickly check if an array has an element or not. First, transform it using the Set constructor and call the method .has()
const mySet = new Set([1, 8, 9, 3, 2])
mySet.has(2) // Result: true
mySet.has(0) // Result: false
2. Sets work with unique elements, imagine you have an array and your goal is to remove all the repeated ones, to do that just transform your array into a set. Doing that you will get an object with unique elements:
let array = [2, 3, 1, 8, 8, 5, 7, 8, 1, 1, 4, 3, 2, 1]
new Set(array) // Set(7) [2, 3, 1, 8, 5, 7, 4]
NOTE: Objects like { foo: 123 }
in a set are not compared by their values but by their references (pointers to the object). That's why i n this particle scenario it will not filter similar objects.
3. Lets say you want to join 2 sets:
let X = new Set([1, 2, 3, 4, 5, 6])
let Y = new Set([5, 6, 7, 8])
X.union(Y) // Set(8) [1, 2, 3, 4, 5, 6, 7, 8]
4. Now if you want the intersection between 2 sets (common elements):
let X = new Set([1, 2, 3, 4])
let Y = new Set([100, 2, 3, 400])
X.intersection(Y) // Set(2) [2, 3]
5. Which elements from X are not in Y? we can respond that question using sets difference, here is an example:
let X = new Set([1, 2, 3, 4])
let Y = new Set([100, 2, 3, 400])
X.difference(Y) // Set(2) [1, 4]
6. How to get elements that are not in common between 2 sets? the answer for that is symmetric difference:
let X = new Set([1, 2, 3, 4])
let Y = new Set([100, 2, 3, 400])
X.symmetricDifference(Y) // Set(4) [1, 4, 100, 400]
7. Can I iterate them? sure:
const mySet = new Set();
mySet.add(1); // Set(1) { 1 }
mySet.add("text"); // Set(2) { 1, 'text' }
mySet.add(true); // Set(2) { 1, 'text', true }
for (const item of mySet) {
console.log(item);
}
// 1, 'text', true
mySet.forEach((value) => {
console.log(value);
});
// 1, 'text', true
Conclusion
Sets are important because they give you a new mindset to solve problems more efficiently. With them, tasks like filtering or searching (depending on the problem, of course) can become easier to handle and can save lines of code. Sets are everywhere and they are very important in computer science, remember that SQL works using sets theory in relational databases π.
Top comments (0)