02.06.2013 Views

DOM Traversal Methods

DOM Traversal Methods

DOM Traversal Methods

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.

Instead of a two-dimensional result array, the map forms a flattened one:<br />

Before: 0,1,52,97<br />

After: -45,0,-44,1,7,2,52,3<br />

$.merge()<br />

Merge the contents of two arrays together into the first array.<br />

$.merge(array1, array2)<br />

Parameters<br />

• array1: The first array to merge<br />

•<br />

array2: The second array to merge<br />

Return Value<br />

An array consisting of elements from both supplied arrays.<br />

[ 179 ]<br />

Chapter 8<br />

Description<br />

The $.merge() operation forms an array that contains all elements from the two<br />

arrays, with duplicates removed. The order of items in the first array is preserved,<br />

with items from the second array appended:<br />

var array1 = [0, 1, 52];<br />

var array2 = [52, 97];<br />

$(this).log('Array 1: ' + array1);<br />

$(this).log('Array 2: ' + array2);<br />

array = $.merge(array1, array2);<br />

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

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

Array 1: 0,1,52<br />

Array 2: 52,97<br />

After: 0,1,52,97<br />

The $.merge() function is destructive. It alters the first parameter to add the items<br />

from the second. If you need the original first array, make a copy of it before calling<br />

$.merge(). Fortunately, $.merge() itself can be used for this duplication:<br />

var newArray = $.merge([], oldArray);<br />

This shortcut creates a new, empty array and merges the contents of oldArray into<br />

it, effectively cloning the array.

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

Saved successfully!

Ooh no, something went wrong!