Skip to content

Instantly share code, notes, and snippets.

@jonalvarezz
Created October 27, 2016 20:20
Show Gist options
  • Save jonalvarezz/011f0129973c0ed1ca4d861581f846a0 to your computer and use it in GitHub Desktop.
Save jonalvarezz/011f0129973c0ed1ca4d861581f846a0 to your computer and use it in GitHub Desktop.
ES6 Array Zip
function zip () {
const args = Array.prototype.slice.call(arguments)
const combinerFunction = args.pop()
const results = []
const min = Math.min(...args.map(a => a.length))
for (let counter = 0; counter < min; counter++) {
let values = args.map(o => o[counter])
results.push(combinerFunction(...values))
}
return results
}
// zip([1, 2], [3, 4], (left, right) => left + right)
// => [4, 6]
//
// zip(['Whats', 'Yup'], ['Up'], ['Man'], (l, c, r) => l + c + r)
// => ['WhatsUpMan']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment