jQuery.unique()Method

jQuery 杂项方法jQuery Miscellaneous Methods

Examples

Remove duplicate div elements from the array

<div>documentcontains6itemsdivblocks</div> <div></div> <div class="dup"></div> <div class="dup"></div> <div class="dup"></div> <div></div> <script> $(function () { //unique() takes an original array var divs = $( "div" ).get(); //Add 3 div block elements divs = divs.concat( $( ".dup" ).get() ); $( "div:eq(1)" ).text( "After reordering, there are" + divs.length + "elements." ); divs = jQuery.unique( divs ); $( "div:eq(2)" ).text( "After reordering, there are" + divs.length + "elements." ) .css( "color", "red" ); }) </script>

Try it Yourself »

Definition and Usage

The $.unique() function is used to sort an array of DOM elements and remove duplicate elements.

Note:1. Only applicable to DOM element arrays, cannot process string or number arrays.
2. Duplicate here means that two elements are actually the same element (determined by strict equality "==="). For example, elements with different nodes but the same attributes are not considered duplicate elements.
3. In jQuery 3.0, this method has been deprecated,and is just an alias of jQuery.uniqueSort(). Please use that method instead.


Syntax

$.unique( array )

Parameter Description
array Array type - the specified DOM element array.


jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions