Copyright © tutorialspoint.com

Prototype Enumerating

previous next


Advertisements

Enumerable class provides a large set of useful methods for enumerations. Enumerations are objects that act as collections 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 of 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 following result:

{ foo: 0, bar: 1, baz: 2}

Using it efficiently:

Here is a complete list of all the methods related to Enumerable.

Prototype Enumerable Methods:

NOTE: Make sure you have at least version 1.6 of prototype.js.

MethodsDescription
all()Determines whether all the elements are boolean-equivalent to true, either directly or through computation by the provided iterator.
any()Determines whether at least one element is boolean-equivalent to true, either directly or through computation by the provided iterator.
collect()Returns the results of applying the iterator to each element. Aliased as map().
detect()Finds the first element for which the iterator returns true. Aliased by the find() method.
each()It lets you iterate over all the elements in a generic fashion, then returns the Enumerable, thereby allowing chain-calling.
eachSlice()Groups items in chunks based on a given size, with last chunk being possibly smaller.
entries()Alias for the more generic toArray method.
find()Finds the first element for which the iterator returns true. Convenience alias for detect().
findAll()Returns all the elements for which the iterator returned true. Aliased as select().
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.
inGroupsOf()Groups items in fixed-size chunks, using a specific value to fill up the last chunk if necessary.
include()Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Aliased as member().
inject()Incrementally builds a result value based on the successive results of the iterator.
invoke()Optimization for a common use-case of each() or collect(): invoking the same method, with the same potential arguments, for all the elements.
map()Returns the results of applying the iterator to each element. Convenience alias for collect().
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.
member()Determines whether a given object is in the Enumerable or not, based on the == comparison operator. Convenience alias for include().
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.
partition()Partitions the elements in two groups: those regarded as true, and those considered false.
pluck()Optimization for a common use-case of collect(): fetching the same property for all the elements. Returns the property values.
reject()Returns all the elements for which the iterator returned false.
select()Alias for the findAll() method.
size()Returns the size of the enumeration.
sortBy()Provides a custom-sorted view of the elements based on the criteria computed, for each element, by the iterator.
toArray()Returns an Array representation of the enumeration. Aliased as entries().
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.

previous next

Copyright © tutorialspoint.com