Keep track of which values you have already processed:
var seen = {};
var values = $('div.bob').map(function() {
var value = this.getAttribute('data-xyz');
// or $(this).attr('data-xyz');
// or $(this).data('xyz');
if(seen.hasOwnProperty(value)) return null;
seen[value] = true;
return value;
}).get();
Reference: $.map()
This, of course, only works with primitive values. In case you are mapping to objects, you'd have to override the toString()
method of the objects so that they can be used as keys.
Or as plugin:
(function($) {
$.fn.map_unique = function(callback) {
var seen = {};
function cb() {
var value = callback.apply(this, arguments);
if(value === null || seen.hasOwnProperty(value)) {
return null;
}
seen[value] = true;
return value;
}
return this.map(cb);
});
}(jQuery));