Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery.unique() Method

The jQuery.unique() method

jQuery Miscellaneous MethodsjQuery Miscellaneous Methods

Example

Remove duplicate div elements from an array

<

div

document

contains

6

items

div

blocks

div

<div div>
<div class="dup" div>
<div class="dup" div>
<div class="dup" div>
<div div>
<script$(function(){//>

unique() gets a raw 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 »


Definition and Usage

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

**Note:** 1. It only applies to arrays of DOM elements; it cannot handle arrays of strings or numbers.
2. Duplicate here means that two elements are actually the same element (determined by strict equality "==="); for example, elements of different nodes with the same attributes are not considered duplicates.
3. In jQuery 3.0, this method was deprecated and is only an alias for jQuery.uniqueSort(). Please use that method instead.

Syntax

$

.unique( array )

Parameters Description
array Array type: The specified array of DOM elements.

jQuery Miscellaneous MethodsjQuery Miscellaneous Methods