Skip to content

Instantly share code, notes, and snippets.

@NickBeeuwsaert
Created July 13, 2015 03:51
Show Gist options
  • Select an option

  • Save NickBeeuwsaert/e64c4b087e399c1a09e4 to your computer and use it in GitHub Desktop.

Select an option

Save NickBeeuwsaert/e64c4b087e399c1a09e4 to your computer and use it in GitHub Desktop.
Python like `zip`-ping
var zip = function() {
var result = [];
// Only zip array-like items
var args = Array.prototype.filter.call(arguments, function(el){
//Make sure the argument is array-like
return typeof el.length !== "undefined";
});
//if we have no items to reduce then return an empty array
if(args.length === 0) return result;
var minLength = args.reduce(function(l, r){
return Math.min(l.length, r.length);
});
// Avoid creating a function within a loop
// Lest Douglas Crockford cast his wrath on me
var extract = function(e) {
return e[this];
};
for(var i = 0; i < minLength; i++) {
result.push(args.map(extract, i));
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment