Web Development with jQuery®

(Elliott) #1

(^162) ❘ CHAPTER 5 ITERATION OF ARRAYS AND OBJECTS


FIGURE 5-11


As you can see, jQuery’s merge() method is pretty straightforward, appending the contents of the
second array argument to the contents of the fi rst array argument.

Summary


This chapter presented several methods associated with iterating and working with arrays
and selections.

You learned how jQuery’s each() method is a less-verbose, easier-to-use alternative for iterating
over an array, object, or selection when compared to using a for construct and a counter, or a for/
in construct for objects. You learned how to emulate break and continue keywords with the each()
method by returning a boolean value. You learned that jQuery’s each() method can be called
directly or chained to a selection or an array that’s wrapped in a call to the dollar sign method.

You learned how a selection is fi ltered using jQuery’s filter() method using either a selector
or a callback function. An array can be fi ltered using jQuery’s grep() method, which must be
called directly.

You learned how one array can be mapped to another array and how one selection can be mapped
to an array using jQuery’s map() method, which exists to translate one set of values to another set
of values.

Finally, you learned about jQuery’s various array utility methods. $.makeArray() can turn any data
into a true array. $.inArray() can fi nd the position of a value within an array, offset from zero, and
works just like JavaScript’s indexOf() method, with –1 (minus one) indicating that a value is not
present within the array. $.merge() glues two separate arrays together into just one array.

http://www.it-ebooks.info

Free download pdf