Issue #484: _.uniq() should work with sparse arrays.

This commit is contained in:
Kit Cambridge
2012-02-20 12:21:36 -07:00
parent 23931b2716
commit c8d4025621
2 changed files with 28 additions and 7 deletions

View File

@@ -371,16 +371,15 @@
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
_.uniq = _.unique = function(array, isSorted, iterator) {
var initial = iterator ? _.map(array, iterator) : array;
var result = [];
_.reduce(initial, function(memo, el, i) {
if (0 == i || (isSorted === true ? _.last(memo) != el : !_.include(memo, el))) {
memo[memo.length] = el;
result[result.length] = array[i];
var results = [], previous;
_.reduce(iterator ? _.map(array, iterator) : array, function (memo, value, index) {
if (array.length < 3 || isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
memo.push(previous = value);
results.push(array[index]);
}
return memo;
}, []);
return result;
return results;
};
// Produce an array that contains the union: each distinct element from all of