تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Creating Maps and Adding Items0:40

In order to create one, we just initialize a variable, and then we call newMap. Now to add an item to a map, we just call that map object, and then the set method on it, which expects two arguments. The first one being the key, and the second one being the value. So if we wanted to, we could just create an id, starting at like 1, and say banana. 2 for apple. And 3 for strawberry. And now we can call a couple of different methods on these maps to perform actions on our data. So let's say that we wanted to pull a specific attribute out of this map. And we'll use our output dom, as we've been doing.

Getting and Deleting Values1:20

So let's say that we wanted to pull a specific attribute out of this map. And we'll use our output dom, as we've been doing. We just call the fruit map, and then the get method, which expects the id, and returns back the value. So if we give it the id of 2, we should be getting back an apple. So let's open up our browser and take a look. There we go, exactly what we expected. We can also delete an item out of the map by calling the delete method. So this will remove the apple that we have in there, and now we should only have two items left.

So this will remove the apple that we have in there, and now we should only have two items left. But their keys don't update, so they should still be 1 and 3. And we can actually loop over this map, just like we would an array. So we can say fruit for each item, and we'll output it to the dom. So now going back into our browser and refreshing, we see apple, which we got before we deleted it, and then the remaining two items from that for each loop. Now like I said earlier though, is that both the key and the value can basically be anything. So we can set number 4 to be an object.

Using Complex Map Keys2:33

Now like I said earlier though, is that both the key and the value can basically be anything. So we can set number 4 to be an object. And so then we can output something from that. So from the fruitMap, we'll get the item that corresponds to the 4 key, and return back the flavor attribute. Using the knowledge that both the key and the value can basically be anything can lead to some interesting combinations though. So let's create a new map. I'll comment out everything that we have so far. And let's call this users.

I'll comment out everything that we have so far. And let's call this users. Like we did before, I'm going to add something to it by calling the set method. But for the key, instead of something simple like an integer or a string, I'm going to use a whole object. And then for the actual value, I'm going to use a function, which uses that object. And we'll have it output something to the DOM. Like the user's name. And email.

Like the user's name. And email. And let's copy this for a second user. We'll change some things up a bit in the key. And you know what, we'll also change something up in the value as well. So we'll swap the email and name attributes in the function that's called. So now running a foreach loop on this, we can use both the value and the key that we get back and call the associated function for each user in the map by just using value and passing in the key. So because for each of these map objects, the value is a function, that is what is associated here.

Checking Keys with has()4:36

So because for each of these map objects, the value is a function, that is what is associated here. And the key that we pass in is each of these User objects that we've set as the keys in this map. So let's see if this works in the browser. And sure enough, there it is. One attribute that I failed to show earlier is that there's also a has method where you can pass in an ID and it will return a Boolean true or false if that item exists in the map. So we can say name, Ashley, email, ashley@example.com. And we'll create a variable for this. And then log that in the console.

ashley at example.com. And we'll create a variable for this. And then log that in the console. So we should get back a true because an item does exist in the map that has this as the key. Let's open up our console in the browser. And interesting, we're getting back a false. And that's because while it is true, an item does exist in the user's map with the ID set as an object or a name and email, which is this exact object here. It's not the same object. So it acts as if they're two different keys. Now if instead I was to go up here and create that object, and then we would set that as that object instead of manually adding it in.

Now if instead I was to go up here and create that object, and then we would set that as that object instead of manually adding it in and then reference it in the user's has. Now let's see what we get back in the browser. So we get a true. And then we get a false. And then we get a false again. And now let's see what we get back in the browser. So the value up here is still the exact same. But now we're getting a true. A User does exist with that object.

Introducing Sets vs Maps6:29

So the value up here is still the exact same. But now we're getting a true. A User does exist with that object because it was the exact same object being used for the key. Okay, I mentioned one other type of special object added. And that's set. Sets are very similar to maps. They both hold items and expose methods to manipulate and iterate over them. But sets differ in a few key areas. So let's go ahead and create one down here. And we'll call it fruit again.

So let's go ahead and create one down here. And we'll call it fruit again. Sets are created with the new Set class. And we add a new object to them by using the add method. So we had set in maps and add in sets. But this is where the key difference is. Unlike maps, sets only contain values. There are no keys associated with sets. So if we wanted to add stuff to our fruit set here, we could just type in a value. But just like our maps, we can loop over these.

So if we wanted to add stuff to our fruit set here, we could just type in a value. But just like our maps, we can loop over these with a foreach loop and output the value to our browser. So back in our browser, if we refresh, we see our three fruit that we have in the set. The other big way that sets differ from maps though is that you can't have duplicate values in them. So if we decided instead of adding one apple, we also added two more. If we go back to the browser and refresh,

Create a MapUse Functions as Values in MapsAdd Data to a Set

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