4

Please, help me to understand, how to use $.unique() function.

From the docs:

Sorts an array of DOM elements, in place, with the duplicates removed.

I'm trying this code:

<h1 class="foo">Headline</h1>
<h1 class="foo">Headline</h1>
<h1 class="foo">Headline</h1>

alert( $.unique($('.foo')).length );

It returns 3, but I supposed 1. What am I missing? And it would be great to see some practice example of using this function. Thanks.

P.S.

I've also tried to sort a few numbers, but got a very curious result. The following code returns different values in different browsers!

$.unique([ 1, 2, 2, 3, 3, 1 ])

  • Safari - 1, 2, 3, 1
  • Opera - 2, 1, 3
  • FF - 3, 2, 1, 3
  • Chrome - 3, 2, 1
  • IE - 2, 1, 3
Arsen K.
  • 5,494
  • 7
  • 38
  • 51

5 Answers5

8

$.unique is only meant for arrays of DOM elements. Not arrays of other things.

In your case, you have 3 <h3>s. They are not the same DOM element, so $.unique doesn't remove them.

<h1 class="foo">Headline</h1>
<h1 class="foo">Headline</h1>
<h1 class="foo">Headline</h1>

alert($.unique($('.foo')).length);  // 3

$.unique is for arrays that may contain the same element multiple times.

<h1 class="foo otherFoo">Headline</h1>
<h1 class="foo">Headline</h1>

var $foo = $('.foo').get();
var $otherFoo = $('.otherFoo').get();

var $e = $foo.concat($otherFoo);
alert($e.length); // 3
alert($.unique($e).length); // 2

On another note, if you want to make $.unique sort arrays of other things, and not just DOMElements, you can do something like this (Taken from here):

(function($){
    var _old = $.unique;
    $.unique = function(arr){
        // do the default behavior only if we got an array of elements
        if (!!arr[0].nodeType){
            return _old.apply(this,arguments);
        }
        else {
            // reduce the array to contain no dupes via grep/inArray
            return $.grep(arr,function(v,k){
                return $.inArray(v,arr) === k;
            });
        }
    };
})(jQuery);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    `$('.foo').add('.otherFoo')` isn't an array, and doesn't contain any duplicates. – Paul Sep 09 '11 at 18:51
  • 1
    In other words, `$.unique()` removes *duplicate* DOM elements, but not *identical* DOM elements. – Blazemonger Sep 09 '11 at 18:54
  • 1
    PaulPRO is right: `$('.foo')` is an array-like jQuery object, not an actual array. However, that fact is secondary in this case. – Blazemonger Sep 09 '11 at 18:55
  • 1
    It's not secondary. `.add()` internally calls `$.unique` so your second example will output 2 whether you call `.unique` or not. (IE. `alert($('.foo').add('.otherFoo').length);` outputs 2. Try it. – Paul Sep 09 '11 at 18:59
  • 1
    I'm don't think it's possible in any context for a jQuery object to contain duplicates, so calling `.unique` on a jQuery object does absolutely nothing. – Paul Sep 09 '11 at 19:00
  • @PaulPRO: You're right. Guess I need a better example of `$.unique`. I'm trying to find a case where you can use `$.unique`. – gen_Eric Sep 09 '11 at 19:01
  • 1
    @Rocket yup, oh well it's an easy mistake to make since `.unique` is such an obscure jQuery function, I'm guessing you've never used it before this example haha, I know I haven't. Not downvote worthy. Probably upvote worthy if you can edit it to include a different example :) – Paul Sep 09 '11 at 19:05
  • @PaulPRO: I just didn't realize `$.unique` only worked on arrays of DOMElements, not jQuery objects. – gen_Eric Sep 09 '11 at 19:15
  • Nice answer. And could you explain, please, what is the difference between $('.foo') and $('.foo').get() ? Because I usually count array length by the former one. – Arsen K. Sep 10 '11 at 07:11
  • @Webars: `$('.foo')` is a jQuery "array-like" object. It has special properties. `.get()` converts a jQuery object into a normal array of DOMElements. – gen_Eric Sep 10 '11 at 16:26
4

$.unique works on an array of DOMElements, not numbers, strings, or a even a jQuery object.

It is used internally by jQuery in add() to prevent duplicates from being added to the same jQuery object. Here is an example of it:

HTML:

<h1 class="foo">Headline</h1>
<h1 class="foo bar">Headline</h1>
<h1 class="bar">Headline</h1>

Javascript:

var foo = $('.foo').get(); // Array of size 2
var bar = $('.bar').get(); // Array of size 2
var myArr = [];
for(var i = 0; i < foo.length; i++)
    myArr.push(foo[i]);
for(i = 0; i < bar.length; i++)
    myArr.push(bar[i]);

alert(myArr.length); // Outputs 4
alert($.unique(myArr).length); // Outputs 3

It should be very rare that you have an ordinary Javascript array of DOMElements though, if you're using jQuery. It is most useful internally within the jQuery source code.

PS. If you want to remove duplicate entries from an array of numbers/strings I recommend using js158's jQuery solution in this question: jQuery function to get all unique elements from an array?

Community
  • 1
  • 1
Paul
  • 139,544
  • 27
  • 275
  • 264
0

Quote from the docs page:

Note that this only works on arrays of DOM elements, not strings or numbers.


So that's why the numbers are different.

The function only removes duplicate DOM elements, not their innerHTML values in case that's what you were wondering, there's examples how to use it over on the documentation page

Clive
  • 36,918
  • 8
  • 87
  • 113
0

From here

Note that this only works on arrays of DOM elements, not strings or numbers.

It also provides an example.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
-3
$.unique(array)

unique is an inbuilt jQuery function to return unique values from an Jaascript array.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
Viraj Kaulkar
  • 301
  • 1
  • 2
  • 15