Fixes #341, flattening arrays in the context of taking a union shouldn't be deep. Adding a shallow parameter to _.flatten()

This commit is contained in:
Jeremy Ashkenas
2011-10-19 15:59:53 -04:00
parent 3587febd8e
commit 4b2de2e107
2 changed files with 13 additions and 7 deletions

View File

@@ -345,9 +345,9 @@
};
// Return a completely flattened version of an array.
_.flatten = function(array) {
_.flatten = function(array, shallow) {
return _.reduce(array, function(memo, value) {
if (_.isArray(value)) return memo.concat(_.flatten(value));
if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
memo[memo.length] = value;
return memo;
}, []);
@@ -377,7 +377,7 @@
// Produce an array that contains the union: each distinct element from all of
// the passed-in arrays.
_.union = function() {
return _.uniq(_.flatten(arguments));
return _.uniq(_.flatten(arguments, true));
};
// Produce an array that contains every item shared between all the