jQuery.unique()
Returns: Array
Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.
The $.unique() function searches through an array of objects, sorting the array, and removing any duplicate nodes. This function only works on plain JavaScript arrays of DOM elements, and is chiefly used internally by jQuery.
As of jQuery 1.4 the results will always be returned in document order.
Examples
Removes any duplicate elements from the array of divs.
var divs = $("div").get();
// add 3 elements of class dup too (they are divs)
divs = divs.concat($(".dup").get());
$("div:eq(1)").text("Pre-unique there are " + divs.length
+ " elements.");
divs = jQuery.unique(divs);
$("div:eq(2)").text("Post-unique there are " + divs.length
+ " elements.")
.css("color", "red");
Full source:
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ var divs = $("div").get(); // add 3 elements of class dup too (they are divs) divs = divs.concat($(".dup").get()); $("div:eq(1)").text("Pre-unique there are " + divs.length + " elements."); divs = jQuery.unique(divs); $("div:eq(2)").text("Post-unique there are " + divs.length + " elements.") .css("color", "red"); }); </script> <style> div { color:blue; } </style> </head> <body> <div>There are 6 divs in this document.</div> <div></div> <div class="dup"></div> <div class="dup"></div> <div class="dup"></div> <div></div> </body> </html>
Was this information helpful?

