collect :: Enumerable

collect(iterator) -> Array Returns the results of applying the iterator to each element. Aliased as map.

This is a sort of Swiss-Army knife for sequences. You can turn the original values into virtually anything!

Examples

['Hitch', "Hiker's", 'Guide', 'To', 'The', 'Galaxy'].collect(function(s) {

return s.charAt(0).toUpperCase();

}).join('')

// -> 'HHGTTG'

$R(1,5).collect(function(n) {

return n * n;

})

// -> [1, 4, 9, 16, 25]

Optimized versions

There are two very common use-cases that will be much better taken care of by specialized variants.

· | First, the method-calling scenario: you want to invoke the same method on all elements,

possibly with arguments, and use the result values. This can be achieved easily with invoke.

· | Second, the property-fetching scenario: you want to fetch the same property on all elements,

and use those. This is a breeze with pluck.

Both variants perform much better than collect, since they avoid lexical closure costs.

See also

Enumerable: pluck | invoke

Prototype API 1.5.0 - prototypejs.org