Transcript
00:00 Alright. Let's work through this one together. So we've got our user object. We've got an array of users, and we're gonna create a map of users keyed by their ID. So, we're going to, get our, well, we can do it all at once.
00:14 Here, that's what the agent wants to do. So here we can say, users by ID. Oh oh, that that's what it's called, array. Here we go. And then we can, put it into our map.
00:26 And we're getting some type issues because, TypeScript can't quite, infer this when we do it this way. So maybe that's why the agent wanted to do it all at once, but we'll we'll work that out here in a second. What I wanted to call out is that we're gonna map the users to a key value pair. I think actually if we did that yeah. As const will fix that.
00:46 We'll talk about as const later on when we get into the type safety stuff. But, yeah. So it's a you basically wanna turn this array into a key value pair. So it's the exact same thing that we did with object from entries. We had key value pairs and that is how the map knows how to set the key and, the value.
01:04 So here we have our user ID and the user and, then when we create the map, we can get users by their IDs. So that's pretty neat. So here we've got our users by ID. If we were to console log that here we'll just console log right here. User by ID.
01:23 Then, we get this, special map structure, where this UID maps to, this object, etcetera, etcetera, etcetera. Okay. Great. So that takes care of that. Then, let's get the admin.
01:38 We'll look up the user by, u one. So that is gonna basically, like, if this were an object, it would be users by ID u one or you just do a dot u one. But with, a a map, you don't have those kinds of property accessors because it's structured differently, so that it can handle lookups a little faster and stuff. So we're gonna get u one and we'll just, unconscile these things. And let's also check whether u 99 exists, with the has method and that's gonna return a boolean on whether or not that property, exists in the map.
02:16 So that is that. That's pretty pretty neat. Now, one other thing that we, didn't have in this, exercise example, but, I thought I'd mention to you, One other thing that's pretty cool is that you can actually have anything as your key. With an object, the key has to be a string. You don't have an option.
02:37 It's gotta be a string. Each, entry in the object has to be, like each property has to be a string of some kind. And we don't quote these but they can be quoted. They they are represented by a string. A map has no such limitations.
02:52 So I could say, admin user, and, we could say, yeah, user ID, etcetera, whatever. We'll call them admin. And then, we can say our, or actually here, let's just do let's do it this way. I'm the admin. There we go.
03:14 And then we can say new or or const admin users, new or equals new map. There we go. So our, it's keyed by our admin user, and the value is, Super secret. This is maybe kind of a confusing example, but my point is that we can later, this is way later, say, Hey, admin users. I wanna get the admin user.
03:46 And this admin user is an object, so we're looking up a value. Yeah. Here we go. SuperSecret. That's their password.
03:52 There we go. That that works. The lookup is happening by using an object and the cool thing about that is we couldn't, do Here. Let's console log that. Admin password.
04:07 Okay. So here's SuperSecret, right? We cannot say IDAdmin. Even though those look the same, we're gonna get undefined because they are not the same object. And so there's some power in having some mechanism for looking or for keying something by an object.
04:26 We actually do this with dom nodes in the browser. Typically for that, you would use a weak map. For reasons that we aren't gonna go into, I recommend you go look it up if you ever end up doing something like this for, memory efficiency reasons and stuff. It's a pretty bit more advanced topic, but, but being able to have an object and say, I wanna store some metadata about that object, put that into a map and then that map can store the metadata and so then you don't have to modify the object to add the metadata. You can have this other sidecar thing that can look up metadata for that object.
05:02 Say, what is the metadata for this and it can go get it and now you've got the metadata. It's quite nice. So, there you go. That is maps. They I I typically for myself, I pretty much am using objects most of the time, but, yeah.
05:16 Occasionally, jumping into a map can be pretty useful. So, I hope that's helpful to you. We'll see you in the next one.
