collect :: Enumerable |
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.
possibly with arguments, and use the result values. This can be achieved easily with invoke.
and use those. This is a breeze with pluck.
Both variants perform much better than collect, since they avoid lexical closure costs.
See also
|
Prototype API 1.5.0 - prototypejs.org