From the course: JavaScript: Maps and Sets
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
Iterate over your set with forEach - JavaScript Tutorial
From the course: JavaScript: Maps and Sets
Iterate over your set with forEach
- [Instructor] Our final lesson on sets will cover the ForEach method. At this point, you should be an expert on ForEach. This method will work as expected with a few minor differences in the callback function. The ForEach method provides a callback function for each value in the set. Just like map, the ForEach method with set accepts four optional parameters. For the sake of this course, I'll only be focusing on three: value1, value2, and set. As we mentioned in the last lesson, the APIs for map and set are the same. Because sets only contains values, both value1 and value2 will be the same. The set parameter represents the entire set that is being looped over. If we take a look at our invite set, we're looping over our set with ForEach. And if the value starts with the letter J, we're going to log it out. Starts with is a JavaScript string method that comes in handy in this particular example. Now let's head to our…
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 Set object defined57s
-
(Locked)
Add values to your set3m 17s
-
(Locked)
Does Set have your value?3m 9s
-
Get your set size2m 2s
-
(Locked)
Delete and clear values in your set2m 46s
-
(Locked)
Iterate over values in a set2m 45s
-
(Locked)
Iterate over a set with entries2m 25s
-
(Locked)
Iterate over your set with forEach2m 29s
-
(Locked)
Solution: Sets 12m 8s
-
(Locked)
Solution: Sets 22m 35s
-
-
-