”;
Enumerable class provides a large set of useful methods for enumerations. Enumerations are objects that act as collection of values.
Enumeration methods are mostly used to enumerate arrays and hashes. There are other objects as well like ObjectRange and various DOM- or AJAX-related objects where you can use enumeration methods.
The Context Parameter
Every method of Enumerable that takes an iterator also takes the context object as the next (optional) parameter. The context object is what the iterator is going to be binded to, so the this keyword inside it will point to the object.
var myObject = {}; [''foo'', ''bar'', ''baz''].each(function(name, index) { this[name] = index; }, myObject); // we have specified the context myObject;
This will produce the following result −
Output
{ foo: 0, bar: 1, baz: 2}
Using it Efficiently
-
When you need to invoke the same method on all the elements, go with invoke() method.
-
When you need to fetch the same property on all the elements, go with pluck() method.
-
The findAll/select methods retrieve all the elements that match a given predicate. Conversely, the reject() method retrieves all the elements that do not match a predicate. In the specific case where you need both the sets, you can avoid looping twice: just use partition() method.
Here is a complete list of all the methods related to Enumerable.
Prototype Enumerable Methods
NOTE − Make sure you at least have the version 1.6 of prototype.js.
S.No. | Method & Description |
---|---|
1. | all()
Determines whether all the elements are boolean-equivalent to true, either directly or through computation by the provided iterator. |
2. | any()
Determines whether at least one element is boolean-equivalent to true, either directly or through computation by the provided iterator. |
3. | collect()
Returns the results of applying the iterator to each element. Aliased as map(). |
4. | detect()
Finds the first element for which the iterator returns true. Aliased by the find() method. |
5. | each()
It lets you iterate over all the elements in a generic fashion, then returns the Enumerable, thereby allowing chain-calling. |
6. | eachSlice()
Groups items in chunks based on a given size, with last chunk being possibly smaller. |
7. | entries()
Alias for the more generic toArray method. |
8. | find()
Finds the first element for which the iterator returns true. Convenience alias for detect(). |
9. | findAll()
Returns all the elements for which the iterator returned true. Aliased as select(). |
10. | grep()
Returns all the elements that match the filter. If an iterator is provided, it is used to produce the returned value for each selected element. |
11. | inGroupsOf()
Groups items in fixed-size chunks, using a specific value to fill up the last chunk if necessary. |
12. | include()
Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Aliased as member(). |
13. | inject()
Incrementally builds a result value based on the successive results of the iterator. |
14. | invoke()
Optimization for a common use-case of each() or collect(): invoking the same method, with the same potential arguments, for all the elements. |
15. | map()
Returns the results of applying the iterator to each element. Convenience alias for collect(). |
16. | max()
Returns the maximum element (or element-based computation), or undefined if the enumeration is empty. Elements are either compared directly, or by first applying the iterator and comparing returned values. |
17. | member()
Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Convenience alias for include(). |
18. | min()
Returns the minimum element (or element-based computation), or undefined if the enumeration is empty. Elements are either compared directly, or by first applying the iterator and comparing returned values. |
19. | partition()
Partitions the elements in two groups: those regarded as true, and those considered false. |
20. | pluck()
Optimization for a common use-case of collect(): fetching the same property for all the elements. Returns the property values. |
21. | reject()
Returns all the elements for which the iterator returned false. |
22. | select()
Alias for the findAll() method. |
23. | size()
Returns the size of the enumeration. |
24. | sortBy()
Provides a custom-sorted view of the elements based on the criteria computed, for each element, by the iterator. |
25. | toArray()
Returns an Array representation of the enumeration. Aliased as entries(). |
26. | zip()
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. |
”;