Web Development with jQuery®

(Elliott) #1

(^140) ❘ CHAPTER 5 ITERATION OF ARRAYS AND OBJECTS
ul = $('ul#beatlesAlbums');
if (albums instanceof Array)
{
console.log("Albums is an array.");
}
else
{
console.log("Albums is a plain object.");
}
// each() called directly.
$.each(
albums,
function()
{
ul.append('

  • ' + this + '
  • ');
    }
    );
    }
    );
    In the preceding example you pass an object to jQuery directly and then try to enumerate the object
    with a call to each(). In Figure 5-2, you can see that the enumeration is not successful. You fi nd that
    only one item called [object Object]. This means that jQuery passed an object to each(), instead of
    looking at each of the four properties attached to the object.


    FIGURE 5-2


    jQuery can enumerate objects, however, as you can see that the second object is enumerated success-
    fully when the object is passed directly to the each() method as its fi rst argument. If an object is an

    http://www.it-ebooks.info

    Free download pdf