From the course: JavaScript: Maps and Sets
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Loop over a map with forEach - JavaScript Tutorial
From the course: JavaScript: Maps and Sets
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?…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
The Map object defined1m 23s
-
(Locked)
Build your Map object with the set method4m 41s
-
(Locked)
Access a value with get3m 51s
-
(Locked)
Does the map have your key?3m 4s
-
(Locked)
Determine map size2m 6s
-
(Locked)
Remove key-value pairs with clear and delete4m 23s
-
(Locked)
Map: Keys and values methods5m 44s
-
(Locked)
Map contents with the entries method2m 32s
-
(Locked)
Loop over a map with forEach2m 38s
-
(Locked)
Group entries with groupBy3m 22s
-
(Locked)
Solution: Maps 12m
-
(Locked)
Solution: Maps 21m 32s
-
-
-
-
-