Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.unique on an array of strings

Tags:

jquery

The description of jQuery.unique() states:

Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.

With the description in mind, can someone explain why the code below works?

<div></div>
<div></div>​

var arr = ['foo', 'bar', 'bar'];

$.each(arr, function(i, value){
    $('div').eq(0).append(value + ' ');
});

$.each($.unique(arr), function(i, value){
    $('div').eq(1).append(value  + ' ');
});
​

http://jsfiddle.net/essX2/

Thanks

Edit: Possible solution:

function unique(arr) {
var i,
    len = arr.length,
    out = [],
    obj = { };

for (i = 0; i < len; i++) {
    obj[arr[i]] = 0;
}
for (i in obj) {
    out.push(i);
}
return out;
};
like image 711
Johan Avatar asked Apr 17 '12 13:04

Johan


People also ask

How do you make an array unique in jQuery?

The $. unique() function searches through an array of objects, sorting the array, and removing any duplicate nodes. A node is considered a duplicate if it is the exact same node as one already in the array; two different nodes with identical attributes are not considered to be duplicates.

How do you find unique elements in an array?

We can use bitwise AND to find the unique element in O(n) time and constant extra space. Create an array count[] of size equal to number of bits in binary representations of numbers. Fill count array such that count[i] stores count of array elements with i-th bit set. Form result using count array.

How to check if an array has duplicate values in jQuery?

just use unique() method.


3 Answers

Although it works, you should probably take into consideration the function description. If the creators say that it is not designed for filtering arrays of anything else than dom elements, you should probably listen to them.
Besides, this functionality is quite easy to be reproduced :

function unique(array){     return array.filter(function(el, index, arr) {         return index === arr.indexOf(el);     }); } 

(demo page)

Update:

In order for this code to work in all browsers (including ie7 that doesn't support some array features - such as indexOf or filter), here's a rewrite using jquery functionalities :

  • use $.grep instead of Array.filter
  • use $.inArray instead of Array.indexOf

Now here's how the translated code should look like:

function unique(array) {     return $.grep(array, function(el, index) {         return index === $.inArray(el, array);     }); } 

(demo page)

like image 179
gion_13 Avatar answered Oct 11 '22 12:10

gion_13


It might work on an array strings, etc, but it has not been designed for that use...

Notice that the code for unique() is hiding in Sizzle as uniqueSort: github source

While some of that extra code might seem like it would work on any array, pay close attention to sortOrder as defined here. It does a lot of extra work to put things in "document order" - hence why the documentation states that it should only be used on arrays of DOM elements.

like image 39
gnarf Avatar answered Oct 11 '22 12:10

gnarf


I know unique works with DOM but this WORKS on arrays of int:

$.unique(arr.sort());
like image 31
evclid Avatar answered Oct 11 '22 10:10

evclid