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;
};
See Question&Answers more detail:os