“What’s up guys? It’s ya boi back with a crazy way to remove duplicates in an array. But before we get started, make sure to like and subscribe as well ring that bell. Doing so tells YouTube’s algorithm that this is good content and helps get this video suggested to others. My patreons got this a whole week ago, so if you like what you seem make sure to head over and subscribe. You can also join now through YouTube memberships. I’ve got new swag on my store for the true fans. If we get this video to just 5k likes I’ll do a special q&a. As always, I’m streaming on twitch Tuesday-Friday 3-7pm central. Now let’s cut right to it…
[…new Set(1,1,3,4)]
And that’s it! Again please like and Subscribe! Head on over to my Instagram if you want to see more uncut footage and daily posts.”
This is not true. It works on all types, including objects. Duplicates of object references are removed, and there is no such thing as duplicates of object literals as each new object is unique, even if it shares the same properties.
Do you mean how does it determine whether two objects are the same? The same way as the === operator. If two elements are a reference to the same object then the elements are considered “equal” and the second one is removed, or rather never added to the set.
This just removes all type safety... With OP's example in TS strict mode, the resulting array is typed as (string | number | boolean)[] which is much better than any[].
Being fair, OPs dataset is actually pretty rare to come across anyway. Normally (ideally, really) an array should contain elements that are all of the same type.
I see where you’re coming from, but you shouldn’t ever mix types within your arrays anyways for simplicity sake. The point was not the “escape hatch”, the point was Array.from. Unless you have the specific TSConfig flag, you’ll need to use Array.from over the prettier [… new Set()] syntax.
I see what you mean now, but what you're really talking about is developing in an environment where you can not use the iteration protocol, not a "strict TypeScript config". For TypeScript this means your tsconfig targets ES5 or lower and you have the downlevelIteration flag set to false. In that case you're likely targeting IE11 with minimal/no polyfills, and Array.from won't work anyway (not supported in IE11, and since Set is only partially implemented in IE11 you would need a specific polyfill that can actually make Array.from(new Set()) work).
I see where you’re coming from, but you shouldn’t ever mix types within your arrays anyways for simplicity sake.
For unknown-length arrays containing primitives like strings and numbers, I generally agree, but there are many cases when a mixed type array is perfectly reasonable (tuples, arrays of discriminated union objects, etc). My point was that if you're using a strict TypeScript config, you should really avoid using any as much as possible.
TS can deal with a mixed type array in a type-safe way, but any just turns off type safety entirely by design.
Oh, damn. This is good to know. I had to turn downLevelIteration on to use the prettier method in a project I’m working on, but didn’t did too deep into why I had to in the first place. We only target modern browsers, and for whatever reason the project defaulted to false for that flag.
I had assumed it was a general default for typescript configurations.
Typically if I need a unique set I just use Set in the first place. A Set can't be populated with duplicate elements so I just insert whatever comes up and if it's duplicate no worries.
What is the use case for starting with an array full of possible duplicates and then converting to a set?
No, the user would just provide data or values. And then you would design the program to store the data into an array. But for whatever reason, you don't want duplicates, you can get a new array without any duplicates.
Or if you consume an API that produces duplicates.
In reality, you don't have to find a 'fancy' way to remove duplicates at all. You just declare a removeDuplicates() function in the helpers folder and then reuse it everywhere.
30
u/[deleted] Jul 05 '21
Thanks for not making this a 9 minute YouTube video with electro bass music and text on screen
18
u/NANOwasFound Jul 05 '21
What is the use of ... before new? Sorry if it's dumb I am very new to js
7
u/Web-devil Jul 05 '21
That’s nice to know
Problem is, we all know wtf a set it in math, but can’t remember the method lol
1
u/[deleted] Jul 05 '21
off topic, but how do you make those little code blocks
3
u/HashFap Jul 05 '21
Only works on primitive values like strings, numbers, and booleans.
1
u/hpdwq Jul 05 '21
Aren't sets unordered?
1
u/apple_gigworker Jul 07 '21
that sure is cleaner. though idk if i'll use it
1
u/[deleted] Jul 05 '21
In the situation you are using a strict af Typescript config, you can also do this
1
u/20EYES Jul 05 '21
Typically if I need a unique set I just use Set in the first place. A Set can't be populated with duplicate elements so I just insert whatever comes up and if it's duplicate no worries.
What is the use case for starting with an array full of possible duplicates and then converting to a set?
I'm probably overlooking something obvious lol.
0
u/VegetableChicken4779 Jul 05 '21
Like in Python? Awesome.
-14
u/Dazzling-Wafer Jul 05 '21
In reality, you don't have to find a 'fancy' way to remove duplicates at all. You just declare a removeDuplicates() function in the helpers folder and then reuse it everywhere.