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

Intro to Maps and Sets

Loading lesson

Transcript

00:00 Welcome to the maps and sets exercise. We're gonna be looking at some maps and making sets. No. Just kidding. This is a different kind of map.

00:06 It's actually a different kind of map from the array map method that we've learned about before. This is a special kind of data structure that you can use to, make your data lookups really efficient and, also, make it so that you can actually use more than just a string as a key. You could have, like, an object and then have a map of metadata and then say, hey, what's the metadata for this object and and go get it. It's it's really cool. Cool stuff that you can do with maps and sets.

00:34 So I'm excited for you to jump into this one. Here's just a quick look at some of the syntax for this. We've got a map here. We're using actually a special feature of typescript called a generic that defines the type of data that's going to live in this in this map. We're not actually gonna touch this because, we'll talk about those types of types later.

00:56 But this is the key type and this is the value type. Here we're saying the key is a string but you can actually use objects like I was saying as the keys which is kinda cool. Then you can set items in the map. You can also initialize a map with that. Here, let's get rid of that.

01:11 You can initialize a map with some data using an array of arrays. So that might look familiar with our from entry stuff that we did. And then you can later then look up items based off of the key that is in the map. These things are really good for caches and that sort of thing. Great.

01:33 Then you can also see whether a map has a particular key. You can delete keys and you can look up their size. On the set side of things, a set is like an array except it's unique version of the array. So here, we've got, two typescripts. If you put that into a set, then those, those version or or the resulting set only has one of those things and then you can turn it into an array using either this spread syntax or you can use array from and now you got a version of that array that doesn't have the duplicates.

02:09 That's pretty much the main reason I ever use a set is to get rid of duplicates. But with those you also have has, add, and delete and you get the size. And it's great. So, we're gonna jump into this. It's gonna be a lot of fun.

02:21 Have a good time with this exercise.