课程: JavaScript: The Tricky Bits

今天就学习课程吧!

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

Solution: Iterate differently

Solution: Iterate differently

(upbeat music) - [Instructor] Let's go over one possible solution for this for a loopless challenge. Here in the browser, I'm going to confirm that it actually works, looking at these various fake towns. Getting pretty sensible numbers down here for total population and mean population. Then over here in my editor, only the JavaScript file changed. So this is the only one we need to look at. I'll collapse down the places array. And we'll go through these steps one by one. The first step was to use array reduce to produce an array of states from that places array. So here is our callback function with the states that we are collecting and the current place. The initial value is an empty array. And so what we're doing here is we're checking to see if that states array contains the current state of the place object of interest, any of these. If it doesn't exist in that array, we add it to the states array. And we just…

内容