Skip to content

Commit

Permalink
Fixes jashkenas#603. Adds an _.zipObject() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed May 23, 2012
1 parent 70d54d0 commit f66f20b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions test/arrays.js
Expand Up @@ -123,6 +123,12 @@ $(document).ready(function() {
equal(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
});

test('arrays: zipObject', function() {
var result = _.zipObject(['moe', 'larry', 'curly'], [30, 40, 50]);
var shouldBe = {moe: 30, larry: 40, curly: 50};
ok(_.isEqual(result, shouldBe), 'two arrays zipped together into an object');
});

test("arrays: indexOf", function() {
var numbers = [1, 2, 3];
numbers.indexOf = null;
Expand Down
14 changes: 13 additions & 1 deletion underscore.js
Expand Up @@ -426,10 +426,22 @@
var args = slice.call(arguments);
var length = _.max(_.pluck(args, 'length'));
var results = new Array(length);
for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
for (var i = 0; i < length; i++) {
results[i] = _.pluck(args, "" + i);
}
return results;
};

// Zip together two arrays -- an array of keys and an array of values -- into
// a single object.
_.zipObject = function(keys, values) {
var result = {};
for (var i = 0, l = keys.length; i < l; i++) {
result[keys[i]] = values[i];
}
return result;
};

// If the browser doesn't supply us with indexOf (I'm looking at you, **MSIE**),
// we need this function. Return the position of the first occurrence of an
// item in an array, or -1 if the item is not included in the array.
Expand Down

0 comments on commit f66f20b

Please sign in to comment.