23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ CONTROLLING FLOW<br />

128<br />

"LunaRacer": 6.6,<br />

"Air Max": 13,<br />

"LunarGlide": 10.2,<br />

"Zoom Streak XC": 7,<br />

"Free": 8.6,<br />

"Mayfly": 4,<br />

"Zoom Vomero": 11.6,<br />

"LunarElite": 9.7<br />

}<br />

var myOptions = [];<br />

for (var shoe in shoes) {<br />

if (shoes[shoe] >= 10) {<br />

myOptions.push(shoe);<br />

}<br />

}<br />

myOptions;<br />

// ["Air Max", "LunarGlide", "Zoom Vomero"]<br />

Figure 4–14. Enumerating members with a for in loop<br />

Our for in loop eliminated the drudgery of having to write the following eight if conditions:<br />

if (shoes["LunaRacer"] >= 10) {<br />

myOptions.push("LunaRacer");<br />

}<br />

if (shoes["Air Max"] >= 10) {<br />

myOptions.push("Air Max");<br />

}<br />

if (shoes["LunarGlide"] >= 10) {<br />

myOptions.push("LunarGlide");<br />

}<br />

if (shoes["Zoom Streak XC"] >= 10) {<br />

myOptions.push(”Zoom Streak XC");<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!