JQuery: utility functions VII – concatenating arrays
In this post we’re going to take a look at the merge utility function. This function expects two arrays that will be merged. The method returns a reference to the first array augmented with the elements contained on the second (which remains unchanged). Here’s a small snippet that shows how to use this function:
var arr = [0, 1, 2];
var anotherArr = [-1, -2, -3];
var merged = $.merge(arr, anotherArr);
The important thing here is that you shouldn’t forget that merged points to arr and that arr now has the items contained by anotherArr. Notice also that this function doesn’t remove duplicates. If you need that you’ll need to use the $.unique method (though it only works with DOM nodes). Keep tuned for more on JQuery.