Duck Punch - jQuery unique()
This is just trying out the idea of duck punching with jQuery by Paul Irish.
http://paulirish.com/2010/duck-punching-with-jquery/
JavaScript
/**
* This is just trying out the idea of duck punching with jQuery by Paul Irish.
* You can read all about it here:
* http://paulirish.com/2010/duck-punching-with-jquery/
* Override jQuery's unique() to have it work with non-DOMElements as well
*/
(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);
$(document).ready(function () {
var x = ['a', 'b', 1, 'a', 2, 'c', 'd', 1, 'c', 'b', 3, 'a', 2, 1],
y = $.unique(x);
$('#u').html(y.join(', '));
});