zip :: Enumerable

zip(Sequence...[, iterator = Prototype.K]) -> Array Zips together (think of the zip on a pair of trousers) 2+ sequences, providing an array of tuples. Each tuple contains one value per original sequence. Tuples can be converted to something else by applying the optional iterator on them.

For those who never encountered a zip function before (i.e. have not worked enough with languages such as Haskell or Ruby ;-)), the exact behavior of this method might be difficult to grasp. Here are a few examples that should clear it up.

Examples

var firstNames = ['Justin', 'Mislav', 'Tobie', 'Christophe'];

var lastNames = ['Palmer', 'Maronhic', 'Langel', 'Porteneuve'];

firstNames.zip(lastNames)

// -> [['Justin', 'Palmer'], ['Mislav', 'Maronhic'], ['Tobie', 'Langel'], ['Christophe', 'Porteneuve']]

firstNames.zip(lastNames, function(a) { return a.join(' '); })

// -> ['Justin Palmer', 'Mislav Maronhic', 'Tobie Langel', 'Christophe Porteneuve']

var cities = ['Memphis', 'Zagreb', 'Montreal', 'Paris'];

firstNames.zip(lastNames, cities, function(p) {

return p[0] + ' ' + p[1] + ', ' + p[2];

})

// -> ['Justin Palmer, Memphis', 'Mislav Maronhic, Zagreb', 'Tobie Langel, Montreal', 'Christophe Porteneuve, Paris']

firstNames.zip($R(1, 100), function(a) { return a.reverse().join('. '); })

// -> ['1. Justin', '2. Mislav', '3. Tobie', '4. Christophe']

Prototype API 1.5.0 - prototypejs.org