Skip to content

Commit

Permalink
Shortened version of unzip that doesn't create sparse arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
almost committed Apr 2, 2013
1 parent 5cbf47e commit 1007164
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,8 @@
// `[['a',1],['b',2],['c',3]]` returns the array
// [['a','b','c'],[1,2,3]].
_.unzip = function(tuples) {
var results = [];
_.each(tuples, function (tuple, tupleIndex) {
_.each(tuple, function (value, itemIndex) {
if (results.length <= itemIndex) {
results[itemIndex] = [];
}
results[itemIndex][tupleIndex] = value;
});
});
return results;
var maxLen = _.max(_.pluck(tuples, "length"))
return _.map(_.range(maxLen), _.bind(_.pluck, null, tuples));
};

// Converts lists into objects. Pass either a single array of `[key, value]`
Expand Down

0 comments on commit 1007164

Please sign in to comment.