Loading
Current section: 7. Maps and Sets 4 exercises
solution

Sets

Transcript

00:00 Alright. Let's jump into this. We're gonna handle unique values with sets, and removing duplicates is, honestly, I think that's the only reason I ever use sets is to handle duplicates. It makes it really easy. So, we're going to take this array of tags and we're going to create unique tags, using a set.

00:21 So we've got TypeScript in here twice. We've got data in here twice and then arrays and objects. So we're gonna, just literally pass that to tags and that's gonna give us a unique, all all the unique tags. And we don't have a console log for that so let's just console log. Right there.

00:36 Ta da. So now we've got a set with TypeScript data and arrays. And then, we can also check whether the, set has TypeScript in it. So, yes, it does. So that's kind of nice.

00:48 And then we are gonna convert this set back into an array. So now we've got our tag list, but this is our unique tag list, which is kind of nice. Here's an array from, the utility that you can use to turn, things back into an array. So if we get rid of this and uncomment that, there we go. We've got our tag list, just the unique values, and here's our unique tag set.

01:13 And, yes, it does have typescript. So, yep. You got your set. You got your has. Very much like an object.

01:20 And the primary beauty of a set is that it, will handle, uniqueness for you automatically. If something already exists in the set, it doesn't add it again. Now, this is just like with objects, and with map where you can map things to objects and there's that referential quality thing we just talked about. You have the same thing here. So if we were to change these, somehow to say, here, let me see if I can make this work.

01:52 Oh man, I messed it up. We're gonna try one more time, multiple cursors and name and then we do this one. Name. Whoop. Name.

02:04 There we go. K. So with that, these are individual objects. Even though they look exactly the same, the set is not going to unique ify those. So our tag list is still gonna have name type script, name data, type script arrays, data objects because these objects are referentially, different or are referentially unique.

02:27 This is actually a concept that, might take a little bit of time and and, like, working through to really get, but it is a really important one to understand in JavaScript. Let's, dig a little bit deeper into that. So let's go, t s is that and data and arrays. And if I were to say, switch these out for those references, now we've got a tags array that has the exact same object twice for both data and t s. And so if I save this, then you notice the tag list only has, one typescript, one data, one arrays, and one objects.

03:04 And the reason for that is because, this is one object that is being referenced twice, whereas before, it was two separate objects. Two separate objects. They're completely different from each other, two different copies, so you've got these objects pointing to different points in memory. It just so happens that those points in memory are exactly the same, the the same, like they look exactly the same, but they're different points in memory. Whereas with this approach, this is, a reference to exactly the same point in memory.

03:36 You just have two references pointing to the same thing, and because they're pointing to the same thing, they're referentially equal and the set will eliminate those. So that is sets. I hope you enjoyed that one.