JS.Next: ES6 / ES2015 Collections
This course has been updated! We now recommend you take the ES6: The Right Parts course.
Transcript from the "Collections" Lesson
>> Aaron Frost: Three collections sets, maps, and weak maps. You guys read a bit about them? Okay, they're kind of like things we already use today, but they're different, so we're going to explain them a little bit. So Set is like an array, Map is like a Key-Value Object that we normally use.
[00:00:22] And then WeakMap is like a Map, but still different still. So let's talk about each one. So sets, we're gonna cruise through them. So set's a unique collection of things, and it will make sure that they're unique. So I just say new Set. I can add 1, 2, and 3.
[00:00:42] I can log the set.size, it tells me there's 3, okay? The reason you'd put something in as a set is because a set has a dot iterable on it, or dot iterator. And you can do some things with iterators and four loops and stuff to get things out of them efficiently.
[00:01:01] So that's why you'd put something in a set as opposed to an array. So it forces unique values, so if I call set.add and I give it 1 three times, the length is still 1, we didn't add it three times like an array would've.
>> Aaron Frost: So if I make a set, and I'm gonna highlight just the code here.
[00:01:25] And if I say has(1) it's gonna false, and then we add 1 and if I say has(1) it's true. So I've now taught you has and add, so now I can call clear, and if I say has(1) it's false again, because it cleared out the set. So the set's now empty.
[00:01:43] That's how you clear out a set is with clear. I can add 1 and 2 to it, and it will tell me the size is 2. And if I delete 2, the size will now be 1, okay? So these are kind of the methods that you put on them.
[00:01:59] Yeah? Okay.
>> Aaron Frost: There's no type casting for uniqueness. So like you might expect it to say maybe a 1 in string 1. Are the same, but they're not, right? So, if I add 1 and if I say has(1), it's true. If I have a string 1, it's false.
[00:02:26] So it's not gonna typecast that. A lot of things in the past to typecast strings and numbers to be interchangeable, but this definitely doesn't. I can then add one and then it will have one at that point, so. So yeah, to iterate over them, if I make a new set, and again, see how I did this?
[00:02:47] I passed an array, you bet you can make a set out of an array or you can do that, add syntax that I showed you, to put things in there. This isn't supported everywhere. So, Chrome has got sets. At least some Chrome has got sets. Because my man's Chrome didn't have sets.
[00:03:03] But I swear I had sets the other day. I could be crazy, though. So some of the constructors don't support this though. So if you try and make it set like this like from an array, it may die. Actually it won't die, it just will ignore it. So it will still run.
[00:03:50] So yeah, iterators are, okay. Iterators kind of go hand in hand with generators. But it will just iterate one by one through the set.