jQuery.unique()S2C Home « Utilities « jQuery.unique()

Array object creation.

Description

The jQuery.unique() jQuery General utility method, returns an array of sorted DOM elements with duplicates removed.

Shorthand version $.unique()

  • The jQuery.unique() jQuery General utility method only works on arrays of DOM elements, not strings or numbers.
  • The returned array will always be in document order.

Syntax


Signature Description
jQuery.unique( array )Return an array of sorted DOM elements with duplicates removed.

Parameters


Parameter Description Type
arrayThe array of DOM elements.Array

Return

An Array object.

jQuery.unique( array ) Examplego to top of page Top

Return an array of sorted DOM elements with duplicates removed.

In the example below when we press the button we create an array variable of the table DOM elements on the page. There are three tables on the page comprised of the 'Syntax' and 'Parameter' tables above, both of which have a class of 'jskeywordtable' and the 'Other Tutorial Sites' table within the footer of the page. We then concatenate onto this array and output a message displaying the array length. After this we use the jQuery.unique() jQuery General utility method on the array, which removes the duplicates we added to the array. Finally we output a message displaying the array length again.



$(function(){
  $('#btn7').one('click', function(){
    var domElem = $('table').get();
    domElem = domElem.concat($('.javatable').get());
    $('#div7').append('<code>domElem</code> contains: ' + domElem.length + ' tables.<br />');
    domElem = jQuery.unique(domElem);
    $('#div7').append('<code>domElem</code> contains: ' + domElem.length + ' tables.<br />');
  });
});

div7. Some initial text.

Press the button below to action the above code:


go to home page Homepage go to top of page Top