JavaScript Set Objects - Episode II - Gettin' Jiggy Wit' it

andershornor

Anders Hornor

Posted on October 2, 2019

JavaScript Set Objects - Episode II - Gettin' Jiggy Wit' it

In my first blog of this series I introduced the Set object. I outlined an instance in which the use of a Set object and its uniqueness check simplified an already overly complex function by making a placeholder hash and two iterative methods unnecessary. In this blog I will to cover some more methods built into the JS Set Objects and where they are useful.

Set.prototype.add(value) && Set.prototype.delete(value)

Like many other data stores Set objects have basic functions do introduce and remove elements from them. They are identical in use to the similar functions for Arrays and Hashes.

let set1 = new Set("set is so cool!")

  #=> Set(9) {"s","e","t"," ","i","o","c","l","!"}

set1.add("Wat?")

  #=> Set(10) {"s","e","t"," ","i","o","c","l","!","Wat?"}

set1.delete("i")

  #=> Set(9) {"s","e","t"," ","o","c","l","!","Wat?"}

Set.prototype.has(value)

Similar to Arrays Set objects have a built in class method that checks for particular values within the Set object. The method returns a boolean value depending on if the value is present(true) or not(false). This is super useful for powerful conditional functions.

if (set1.has("i")) {
  set1.delete("Wat?")
  set1.add("WAT!?")
}

console.log(set1)

  #=> Set(9) {"s","e","t"," ","o","c","l","!","Wat?"}

Set.prototype.clear()

As described in my first blog of this series Set objects are akin to a Hash Array hybrid in a way. Unlike either Set objects have an included function that works to empty the elements of the Set.

if (set1.has("Wat?")) {
  set1.clear()
}

  #=> Set(0)

Set.prototype.entries()

The Set object's built in class methods also break the Array-Hash paradigm with the Set.prototype.entries()method. This class method returns a SetIterator object that contains all the elements of the original Set object as key-value pairs where each key and value are the element (maybe succinct but definitely confusing examples FTW).

set2 = new Set("set is so cool!")

  #=> Set(9) {"s","e","t"," ","o","c","l","!"}

entries = set2.entries()

console.log(entries)

  #=> SetIterator {"s" => "s", "e" => "e", "t" => "t", " " => " ", "i" => 
  "i", "o" => "o", "c" => "c", "l" => "l", "!" => "!"}

Set.prototype.values() &&/|| Set.prototype.keys()

Back into the Hash-Array relationship Set objects have a similar function to hashes that returns the values or keys of the Set. Unlike Hashes the Set.prototype.values() and Set.prototype.keys() both return the same SetIterator object containing all of the elements of the original Set even when key-value pairs are present. Whence I am a famous programmer I will return to this post and explain why a SetIterator that is identical to the set its a part of. My inkling is that it has to do with efficiency. SetIterator objects have one class method, SetIterator.prototype.next(). ~My assumption~ is that they take less memory to store because they are similar to linked lists. Anyway, examples:

set2.values()

  #=> SetIterator {"s","e","t"," ","o","c","l","!"}

set2.keys()

  #=> SetIterator {"s","e","t"," ","o","c","l","!"}

let set3 = new Set([{"key": "value"},{"keys": "values"}])

set3.keys()

  #=> SetIterator {{"key": "value"},{"keys": "values"}}

set3.values()

  #=> SetIterator {{"key": "value"},{"keys": "values"}}

Next Time!

That is about it for Set. Thank you for checking out my blog! I plan to revisit this blog in the coming months when I am more knowledgeable about Set and all its wonderful uses. Till next time!

💖 💪 🙅 🚩
andershornor
Anders Hornor

Posted on October 2, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related