Reputation: 35194
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 + ' ');
});
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;
};
Upvotes: 46
Views: 106056
Reputation: 41
OK, so without Sizzle a shorthand for jQuery will be appreciate!
Based on the gion_13 answer, I ve just rewrite to make a native Array implementation.
Array.prototype.unique = function() {
let _array = this;
return $.grep(_array, function(el, index) {
return index === $.inArray(el, _array);
});
};
Edited to be consistent ES shorter...
Array.prototype.unique=function(){let _array=this;return $.grep(_array,function(el, index){return index===$.inArray(el,_array);});};
In this case test done:
var A = ['1', 'green', true, 'blue', 1, (!false), 'toto', 'green', (!!0) ];
console.log( A );
// Array(9) [ "1", "green", true, "blue", 1, true, "toto", "green", false ]
console.log( A.unique() );
//Array(7) [ "1", "green", true, "blue", 1, "toto", false ]
Note about evclid that $.unique(arr.sort());
works fine but we lose order (:
Upvotes: 0
Reputation: 69
var array=['a','b','c','a'];
function unique(array)
{
var unique_arr=[];
array.forEach(function(i,e)
{
if(unique_arr.indexOf(i)===-1) unique_arr.push(i);
});
return unique_arr;
}
console.log(unique(array));
Upvotes: 0
Reputation: 350
If not limited using jQuery, consider to use Set from ES6.
var arr = ['foo', 'bar', 'bar'];
Array.from(new Set(arr)); // #=> ["foo", "bar"]
Working for Firefox 45, Safari 9 and Chrome 49.
Upvotes: 9
Reputation: 41533
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);
});
}
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 :
Now here's how the translated code should look like:
function unique(array) {
return $.grep(array, function(el, index) {
return index === $.inArray(el, array);
});
}
Upvotes: 105
Reputation: 3339
There's a quick way to extend the jQuery.unique() function to work on arrays containing elements of any type.
(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);
// in use..
var arr = ['first',7,true,2,7,true,'last','last'];
$.unique(arr); // ["first", 7, true, 2, "last"]
var arr = [1,2,3,4,5,4,3,2,1];
$.unique(arr); // [1, 2, 3, 4, 5]
http://www.paulirish.com/2010/duck-punching-with-jquery/ - example #2
Upvotes: 3
Reputation: 171
I know unique works with DOM but this WORKS on arrays of int:
$.unique(arr.sort());
Upvotes: 15
Reputation: 361
$.unique will only remove duplicate DOM element, if you need it for array :
var result=[] ;
$.each([12,1,2,4,3,1,4,3,3,2,111],
function(i,e){ if($.inArray(e,result)===-1) result.push(e) ;});
result;
Upvotes: 0
Reputation: 92893
$.unique
will remove duplicate DOM elements, not identical DOM elements. When you try to use it on strings, you get unpredictable behavior and the sorting will (probably) fail.
It's a function intended for internal use by jQuery only, and won't be useful to mere mortals like you and I.
Upvotes: 4
Reputation: 106332
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.
Upvotes: 23