در حال بارگذاری ...

Set Basics and API0:00

Next up in our learning, we have sets. And these are very simple. They are collections of values where each one must be unique. So you'll get a lot of use out of these. So for example, if I were just to say one, two, three, and then I'll show you some more actual examples. But anyways, if we were to save that to items, and then console.log the items, and let's switch over. Sure enough, we have a set of three items. Now though, if I were to say one, two, three again, once again, they must be unique, so these will be ignored. So if I switch over, yep, we still have the exact same three items. And now we save that to a variable called items, right? So we can work from here. We can fetch the size using the API, and we get three. We can add new items. So I could say items.add(4). We can delete items by their value, items.delete(2). And now we should have a size of three, where we have one, three, and four. We can check if the

new items. So I could say items.add4. We can delete items by their value, items.delete2. And now we should have a size of three, where we have one, three, and four. We can check if the set has an item. So we could say items.has4, and that will return true, of course. But it does not have five. Next, of course, they are iterable. So I could say items.forEachItem, console.log the item, or whatever you need to. And we get each of the three items. And of course, we can clear them out entirely so that we have no more, and we can start from scratch. So you can see what we did is when we instantiated a set object, we passed through an array to the constructor. But yeah, of course, you can do it one at a time. And of course, you can grab the values, and that will give you an iterator that you can then use with forOf, or that will be done by default. So for example, I could say, let's add one more. We could say forLetItemOfSet, then console.log the item.

Using Sets for Uniqueness1:39

give you an iterator that you can then use with forOf, or that will be done by default. So for example, I could say, let's add one more. We could say forLetItemOfSet, then console.log the item. And we are iterating over those. But like I said, you could use forOf, or the set API includes forEach method that you can use. So here's a couple examples you might consider. Maybe you have a form where the user can type in a comma-separated list of tags. Maybe they're publishing a post. And you've kept it very simple. So it's just an input where they type in a comma-separated list of tags. So let's just call it tags. And once again, the user types in, how about PHP, JavaScript, Vue, but then they accidentally type in JavaScript again, or something like that, right? Well, now this isn't what we want. We have an array of four items, but really, it should just be three because we have duplicates. So yeah, you could iterate over these and kind of strip the one out.

Well, now this isn't what we want. We have an array of four items, but really, it should just be three because we have duplicates. So yeah, you could iterate over these and kind of strip the one out that's duplicated dynamically. But with sets, you just get this out of the box. So I could say new Set of tags. We'll save that to set. And if we console.log that, once again, we'll have an array of only the three items, and any duplicates will be stripped out. Now on this note, though, what if you need to do some more functional type things, like you want to filter down this set to only those that are three letters. So you only want PHP and JavaScript. Well, set doesn't include any of those methods on its API. So if we take a look, here's the general API you have here. So if you do want to use filter, what you would likely need to do is convert it to an array, and then call filter on the array. And then you could turn that result back into a set.

Filtering Sets via Arrays3:20

So if you do want to use filter, what you would likely need to do is convert it to an array, and then call filter on the array. And then you could turn that result back into a set. So something like this. So we have our set variable, and if we want to turn it into an array, we can use the spread operator that we've already learned about. So now we have an array of the three items, which means, yeah, I could say filter those down where the tag's length is equal to three. And that should give us just two. So then you take that and you convert it back into a set. Something like this. That's kind of what you would do in that particular case. Maybe a little bit messy, but you know what, I don't think it's too bad. What about another example? Maybe you have a forum and the users can mention others. So for example, if I were to say, John Doe, did you see this? Check with Jane Doe, you know. And basically, we want to dynamically find any mentioned users.

Deduplicating Mentions Example4:54

unique is only one bullet point to a set. There's a number of reasons to use it. Anyways, in the past, you might have reached for like a library, you could do it yourself, of course, or often people will use things like underscore. So you might say, give me the unique items from mentionedUsers. And then you could say var uniqueMentionedUsers, that would be using the underscore library. But yeah, once again, you would just say, give me a new set of mentionedUsers. And that will automatically strip out any non unique ones. And that will automatically strip out any that are not unique. So we should have John Doe and Jane Doe. And we do. And that sets in ECMAScript 2015.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟