09.02.2015 Views

DOM Traversal Methods - MarkMail

DOM Traversal Methods - MarkMail

DOM Traversal Methods - MarkMail

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Miscellaneous <strong>Methods</strong><br />

$.unique()<br />

Creates a copy of an array of objects with the duplicates removed.<br />

$.unique(array)<br />

Parameters<br />

• array: An array of objects<br />

Return Value<br />

An array consisting of only unique objects.<br />

Description<br />

The $.unique() function searches through an array of objects, forming a new<br />

array that does not contain duplicate objects. Two objects are considered distinct if<br />

they refer to different locations in memory, even if their contents are identical. The<br />

original array is not modified. The array may consist of any kind of JavaScript object:<br />

var alice = {'alice’: 'alice’};<br />

var bob = {'bob’: 'bob’};<br />

var carol = {'carol’: 'carol’};<br />

var ted = {'bob’: 'bob’};<br />

var oldArray = [alice, bob, carol, bob, ted];<br />

$(this).log('Before: ' + oldArray);<br />

newArray = $.unique(oldArray);<br />

$(this).log('After: ' + newArray);<br />

The resulting array contains only the four distinct items:<br />

Before: {alice: alice}, {bob: bob}, {carol: carol},<br />

{bob: bob}, {bob: bob}<br />

After: {alice: alice, mergeNum: 52}, {bob: bob, mergeNum: 52},<br />

{carol: carol, mergeNum: 52}, {bob: bob, mergeNum: 52}<br />

The second instance of the object named bob is removed from the resulting array.<br />

However, the object named ted remains even though it has identical content, since it<br />

was created as a separate object.<br />

Note that $.unique() modifies the objects in the array, adding an extra property<br />

called mergeNum to each. This property is a side effect of the implementation of the<br />

function, and is not useful to the calling code.<br />

[ 180 ]

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

Saved successfully!

Ooh no, something went wrong!