JavaScript Sets are Excellent!
See the original post here: https://dev.to/jacobmgevans/javascript-sets-are-excellent-1bh7
Sets In JavaScript Are 🔥
- In MDN Set it states "The Set object lets you store unique values of any type, whether primitive values or object references."
So the obvious use case for Sets, removing duplicates! Which they do very easily and concisely. I personally like the example I created for the banner. The combination of spread ... and new Set() inside of an array literal consuming input from the function, meaning it will take in any iterable that can be spread "dedupe" it and return those values in an array! Super cool!! A lot going on in a small package.
Quasi-Typecheck/searching & searching & deleting
- Sets in JavaScript have some things Arrays don't have and are missing some things Arrays do have... Example, Sets have no index! "But I thought you said it was iterable?" I did, that doesn't mean it has an Index 😆
- Ok, so what is one of the most useful things all iterables share! I want to know it "has" 😆 something, with Objects .hasOwnProperty() will check if something exists but not if the type matches...
- So... Not only does the .has() from Sets check work a little differently than the one from .hasOwnProperty() but it also works a bit differently than .includes() from arrays but both .has() & .includes() will return false of the type does not match what is being search for.
- The next super useful thing from built-in of Sets and there isn't the equivalent in Arrays is .delete() which in Arrays you either know the index or search for and get the index of the element then work out how to remove it.
- What does Set .delete() look like in comparison?
A great resource to read on Sets as well!
https://medium.com/front-end-weekly/es6-set-vs-array-what-and-when-efc055655e1a
🚧🚧🚧🚧
More to come on Set use cases and examples, with explanations 😎
🚧🚧🚧🚧
A lesson for next time, don't use image links lol