JavaScript Sets are Excellent!

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

Like
Reply

To view or add a comment, sign in

More articles by Jacob M-G E.

  • Anagram Generator

    See Original Post: https://dev.to/jacobmgevans/anagram-generator-5ej9 TL;DR I did a challenge for an anagram creator, I…

  • JavaScript Resources - Podcasts, Books, Videos and Tutorials

    Whether you are new to developing or you are a seasoned software developer there are still some resources that can be…

    2 Comments
  • Tech Should Teach Others

    This will be an ongoing article, as I research more into other incidents and potential positive impact solutions we all…

    1 Comment
  • Art of The Mind’s Eye

    Author: Jacob Evans Co-Author: Joshua Evans “A Piece of Land,” an exhibition of photography presented by Andrzej…

  • The Masks of Psychopaths

    The psychopath, as differentiated from Antisocial Personality Disorder to wit psychopathy focusing more on the cause…

  • Samurai: Japan’s Mirror

    The samurai culture’s role in society was a mirror for Japanese aristocracy and later a role model for the rest of…

  • Gamification And Crowdsourcing

    Gamification had made the headlines a few times in recent years, and most people probably didn’t think much of it, I…

Explore content categories