This unique() Method in jQuery is used to sorts an array of DOM elements, in place, with the duplicates removed.
Syntax:
jQuery.unique( array )
Parameters: The unique() method accepts only one parameter that is mentioned above and described below:
- array : This parameter is the Array of DOM elements.
Return Value: It returns the sorted array of DOM elements after removing the duplicates.
Example 1: In this example, the unique() method removes duplicate elements from the array of divs.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" >
< title >JQuery | unique() method</ title >
</ script >
< style >
div {
color: blue;
}
</ style >
</ head >
< body style = "text-align:center;" >
< h1 style = "color: green" >
GeeksForGeeks
</ h1 >
< h3 >JQuery | unique() method</ h3 >
< div ></ div >
< div class = "geek" ></ div >
< div class = "geek" ></ div >
< div class = "geek" ></ div >
< div ></ div >
< script >
$(function () {
var divs = $( "div" ).get();
divs = divs.concat( $( ".geek" ).get() );
$( "div:eq(1)" ).text(
"Sorts an array of DOM elements " + divs.length +
" with the duplicates removed" );
divs = jQuery.unique( divs );
$( "div:eq(2)" ).text(
"Sorts an array of DOM elements " + divs.length +
" with the duplicates removed" )
.css( "color", "red" );
})
</ script >
</ body >
</ html >
|
Output:
Example 2: In this example, the unique() method removes all duplicate elements from the array of p’s.
<!DOCTYPE html>
< html >
< head >
< meta charset = "utf-8" >
< title >JQuery | unique() method</ title >
< script src =
</ script >
</ head >
< body style = "text-align:center;" >
< h1 style = "color: green" >
GeeksForGeeks
</ h1 >
< h3 >JQuery | unique() method</ h3 >
< p ></ p >
< p class = "geek" ></ p >
< p ></ p >
< script >
$(function () {
var ps = $( "p" ).get();
ps = jQuery.unique( ps );
$ UniqueSort (document getElementsByTagName ( "p".));
$( "p" ).text(
"Sorts an array of DOM elements with the duplicates removed" )
.css( "color", "red" );
})
</ script >
</ body >
</ html >
|
Output:
Last Updated :
27 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...