During our recent mod2 project we ran into an issue of needing to return a unique list while using Javascript. After doing a little research we were able to discover the world of Set(). Set() was very useful in that it allows you to create a collection of items that are unique.
We were able to assign a variable to new Set(), which creates an empty set. Then using set().add(value) we were able to add a unique list of values to a collection. Conveniently Set() returns this new list as an object. Once you have this object you are able to use forEach in order to iterate through your object and apply specific functions.
Set() also has a few other uses. You are able to delete elements from the collection in one of two ways. You can either use set().delete(value) which will return a boolean response regarding whether or not the value was deleted. Alternatively you can use set().clear in order to clear the entire collection, which will return an undefined value.
This is merely brushing the surface of the functionality while using Set(). I highly recommend doing a little research if you find yourself in need of a unique list while using javascript!
Top comments (0)