课程: JavaScript: Maps and Sets

今天就学习课程吧!

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

Loop over a map with forEach

Loop over a map with forEach

- [Instructor] The last method that we will discuss is ForEach. This method should look pretty familiar to you as it is also a JavaScript array method and it operates in the same manner. The ForEach method executes a function for each key value pair in the map. The ForEach method accepts four optional parameters. For the sake of this course, I'm only going to focus on three of them: value, key, and map. Each of these parameters are pretty explicit as to what they represent. Value is the value of each iteration. Key is the key of each iteration, and map is the map being iterated over. Let's go to our meals map one last time and see how we can use this ForEach method. In this example, we are just going to focus on using the parameters value and key. We take our meals map and apply ForEach, passing in value and key as parameters. Each time it iterates, we will log out I am having a key for value. Pretty cool, right?…

内容