The jQuery.uniqueSort() method
Example
Remove duplicate div elements from an array
<
div
There are 6 div blocks in the document
div
<div div>
<div class="dup" div>
<div class="dup" div>
<div class="dup" div>
<div div>
<script$(function(){//>unique() must be given 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
.
uniqueSort
(
divs
)
;$.
uniqueSort
(
document
.
getElementsByTagName
(
"
div
"
)
)
; $
(
"
div:eq(2)
"
)
.
text
(
"
After reordering, there are
"
divs
.
length
"
elements
"
)
.
css
(
"
color
"
,
"
red
"
)
;
}
)
script
Try it »
Definition and Usage
$The .uniqueSort() function sorts the array by searching through the array object and removes any duplicate nodes.
**Note:** Two different nodes with the same attributes are considered non-duplicates. This function only applies to arrays of ordinary JavaScript DOM elements and is mainly used internally by jQuery.
Before jQuery 3.0, this method was called jQuery.unique().
Syntax
$
.uniqueSort(array)
| Parameters | Description |
|---|---|
| *array * | Array type: The specified array of DOM elements. |
jQuery Miscellaneous Methods