Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You have two jQuery DOM collections that need to have duplicate elements removed:
(function($) {
$(document).ready(function() {
var animals = $('li.animals').get();
var horses = $('li.horses').get();
$('#animals')
.append( $(animals).clone() )
.append( $(horses).clone() );
});
})(jQuery);(function($) {
$(document).ready(function() {
var animals = $('li.animals').get();
var horses = $('li.horses').get();
var tmp = $.merge( animals, horses );
tmp = $.unique( tmp );
$('#animals').append( $(tmp).clone() );
});
})(jQuery);jQuery’s $.unique() function will remove duplicate DOM elements from an array or collection. In the previous recipe, we combine the animals and horses arrays using $.merge(). jQuery makes use of $.unique() throughout most of its core and internal functions such as .find() and .add(). Thus, the most common use case for this method is when operating on an array of elements not constructed with jQuery.