课程: JavaScript: Maps and Sets

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Build your Map object with the set method

Build your Map object with the set method - JavaScript教程

课程: JavaScript: Maps and Sets

Build your Map object with the set method

- [Instructor] As mentioned in the previous lesson, a map does not contain keys by default. In this lesson, we're going to discuss how to properly store data in a map using the method set. The set method is what you will use to create key value pairs in your map. The syntax is straightforward, mapObject.set(key, value). Unlike a regular object, our key can be anything, which is a pretty powerful thing. Let's work through an example. I have some emojis that I want to associate with different meals throughout my day. The apple represents breakfast, a slice of pizza for lunch, a hamburger for dinner, and a cookie represents dessert. How might we construct a map using this information? First, we decide that the emojis are going to be our keys since we can use anything as our key, then whatever meal the emoji is associated with will be the value. To start building the map, we will need to use set to add the key value pairs.…

内容