课程: JavaScript: Maps and Sets

今天就学习课程吧!

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

Solution: Sets 1

Solution: Sets 1

(upbeat music) - [Instructor] In this challenge, we're given an array of pet names and we need to create a new set based on this array. So the first thing that we'll need to do is actually establish our new set. So I want to point out from the beginning that there could be a couple different ways that you achieve the correct solution in this particular challenge. My way is not the only way. So keep that in mind when you are working through this. So the first thing that I'm going to do is I'm going to create a new variable and I'm going to call it petSet, and I'm going to set that equal to a new Set. Then I know I want to add each item that's in this namesForFamilyPet array. I want to add it to this new set. So what I'm going to do is actually iterate over this array, and, for each name, I'm going to add it to the set. So we will actually utilize our array methods here. So namesForFamilyPet and we're going to to use forEach here. And I'm going to say forEach(name) and I'm going to…

内容