instance method Enumerable#collect

View source on GitHub →

Enumerable#collect([iterator = Prototype.K[, context]]) → Array
  • iterator (Function) – The iterator function to apply to each element in the enumeration.
  • context (Object) – An optional object to use as this within calls to the iterator.

Returns the result of applying iterator to each element. If no iterator is provided, the elements are simply copied to the returned array.

Examples
['Hitch', "Hiker's", 'Guide', 'to', 'the', 'Galaxy'].collect(function(s) {
  return s.charAt(0).toUpperCase();
});
// -> ['H', 'H', 'G', 'T', 'T', 'G']
 $R(1,5).collect(function(n) {
  return n * n;
});
// -> [1, 4, 9, 16, 25]

Aliased as: Enumerable#map