课程: JavaScript: The Tricky Bits
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
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…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
Pain point: Sequential for loops become tedious3 分钟 52 秒
-
Solution: Three alternatives8 分钟 10 秒
-
(已锁定)
Pain point: Processing arrays with loops is inelegant3 分钟 22 秒
-
(已锁定)
Solution: filter(), map(), and reduce()11 分钟 3 秒
-
(已锁定)
Challenge: Iterate differently3 分钟 32 秒
-
(已锁定)
Solution: Iterate differently3 分钟 6 秒
-
-
-
-