Created
October 27, 2016 20:20
-
-
Save jonalvarezz/011f0129973c0ed1ca4d861581f846a0 to your computer and use it in GitHub Desktop.
ES6 Array Zip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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