课程: JavaScript: Maps and Sets

今天就学习课程吧!

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

Iterate over your set with forEach

Iterate over your set with forEach - JavaScript教程

课程: 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…

内容