<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - Enumerable</title>
  <id>tag:prototypejs.org,2011:mephisto/api/enumerable</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://prototypejs.org:80/feed/api/enumerable/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://prototypejs.org:80/api/enumerable" rel="alternate" type="text/html"/>
  <updated>2007-06-21T04:01:30Z</updated>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Christophe</name>
    </author>
    <id>tag:prototypejs.org:80,2007-03-18:14930</id>
    <published>2007-03-18T17:09:00Z</published>
    <updated>2007-06-21T04:01:30Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/inGroupsOf" rel="alternate" type="text/html"/>
    <title>inGroupsOf</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;inGroupsOf(size[, filler = null]) -&gt; [group...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Groups items in fixed-size chunks, using a specific value to fill up the last chunk if necessary.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;inGroupsOf(size[, filler = null]) -&gt; [group...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Groups items in fixed-size chunks, using a specific value to fill up the last chunk if necessary.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var students = [
  { name: 'Sunny', age: 20 },  { name: 'Audrey', age: 21 },
  { name: 'Matt', age: 20 },   { name: 'Élodie', age: 26 },
  { name: 'Will', age: 21 },   { name: 'David', age: 23 },
  { name: 'Julien', age: 22 }, { name: 'Thomas', age: 21 },
  { name: 'Serpil', age: 22 }
];

students.pluck('name').inGroupsOf(4) {
// -&gt; [ ['Sunny', 'Audrey', 'Matt', 'Élodie'],
//      ['Will', 'David', 'Julien', 'Thomas'],
//      ['Serpil', null, null, null] ]
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;There is a variant called &lt;a href=&quot;/api/enumerable/eachSlice&quot;&gt;&lt;code&gt;eachSlice&lt;/code&gt;&lt;/a&gt;, which does not fill up the last group to size.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Christophe</name>
    </author>
    <id>tag:prototypejs.org:80,2007-03-18:14929</id>
    <published>2007-03-18T17:00:00Z</published>
    <updated>2007-11-15T16:50:33Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/eachSlice" rel="alternate" type="text/html"/>
    <title>eachSlice</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;eachSlice(size[, iterator = Prototype.K[, context]]) -&gt; [slice...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Groups items in chunks based on a given size, with last chunk being possibly smaller.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;eachSlice(size[, iterator = Prototype.K[, context]]) -&gt; [slice...]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Groups items in chunks based on a given size, with last chunk being possibly smaller.&lt;/p&gt;
&lt;p&gt;Sometimes, you want to cut collections into chunks. Roughly equal-sized. Maybe you want to put it into multiple columns, or some other stylish layout. Maybe you can only pass so many at a time to a back-end processing layer (aaaah, those hard-coded, arbitrary limits in legacy software&#8230; ). Maybe you just feel like it. Just use &lt;code&gt;eachSlice&lt;/code&gt; or its fixed-size variant, &lt;a href=&quot;/api/enumerable/inGroupsOf&quot;&gt;&lt;code&gt;inGroupsOf&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The optional &lt;code&gt;context&lt;/code&gt; parameter is what the iterator function will be bound to. If used, the &lt;code&gt;this&lt;/code&gt; keyword inside the iterator will point to the object given by the argument.&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var students = [
  { name: 'Sunny', age: 20 },  { name: 'Audrey', age: 21 },
  { name: 'Matt', age: 20 },   { name: 'Élodie', age: 26 },
  { name: 'Will', age: 21 },   { name: 'David', age: 23 },
  { name: 'Julien', age: 22 }, { name: 'Thomas', age: 21 },
  { name: 'Serpil', age: 22 }
];

students.eachSlice(4, function(toon) {
  return toon.pluck('name');
})
// -&gt; [ ['Sunny', 'Audrey', 'Matt', 'Élodie'],
//      ['Will', 'David', 'Julien', 'Thomas'],
//      ['Serpil'] ]

students.eachSlice(2).first()
// -&gt; [{ name: 'Sunny', age: 20 }, { name: 'Audrey', age: 21 }]
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;A common use-case for &lt;code&gt;eachSlice&lt;/code&gt;, which mandates fixed-size groups, thereby requiring padding of the last one if necessary, is available through &lt;a href=&quot;/api/enumerable/inGroupsOf&quot;&gt;&lt;code&gt;inGroupsOf&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Christophe</name>
    </author>
    <id>tag:prototypejs.org:80,2007-01-06:12976</id>
    <published>2007-01-06T12:18:00Z</published>
    <updated>2007-01-06T14:14:48Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/size" rel="alternate" type="text/html"/>
    <title>size</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;size() -&gt; Number&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the size of the enumeration.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;size() -&gt; Number&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the size of the enumeration.&lt;/p&gt;
&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$R(1, 10).size()
// -&gt; 10

['hello', 42, true].size()
// -&gt; 3

$H().size()
// -&gt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Performance considerations&lt;/h3&gt;

&lt;p&gt;This method exists solely to provide a generic size-getting behavior for all objects enumerable.  The default implementation actually performs the loop, which means it has exact linear complexity.  Most objects that mix in &lt;code&gt;Enumerable&lt;/code&gt; will try to optimize this by redefining their own version of &lt;code&gt;size&lt;/code&gt;; this is, for instance, the case of &lt;a href=&quot;/api/array&quot;&gt;&lt;code&gt;Array&lt;/code&gt;&lt;/a&gt;, which &lt;a href=&quot;/api/array/size&quot;&gt;redefines&lt;/a&gt; &lt;code&gt;size&lt;/code&gt; to delegate to arrays&#8217; native &lt;code&gt;length&lt;/code&gt; property.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12351</id>
    <published>2006-10-29T22:16:00Z</published>
    <updated>2007-11-15T16:49:58Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/each" rel="alternate" type="text/html"/>
    <title>each</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;each(iterator[, context]) -&gt; Enumerable&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The cornerstone of &lt;code&gt;Enumerable&lt;/code&gt;.  It lets you iterate over all the elements in
a generic fashion, then returns the &lt;code&gt;Enumerable&lt;/code&gt;, thereby allowing chain-calling.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;each(iterator[, context]) -&gt; Enumerable&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The cornerstone of &lt;code&gt;Enumerable&lt;/code&gt;.  It lets you iterate over all the elements in
a generic fashion, then returns the &lt;code&gt;Enumerable&lt;/code&gt;, thereby allowing chain-calling.&lt;/p&gt;
&lt;p&gt;Iterations based on &lt;code&gt;each&lt;/code&gt; are the core of &lt;a href=&quot;/api/enumerable&quot;&gt;&lt;code&gt;Enumerable&lt;/code&gt;&lt;/a&gt;.
The iterator function you pass will be called with two parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The current element in the iteration.&lt;/li&gt;
&lt;li&gt;The numerical index, starting at zero, of the current cycle.  This second
parameter is unused (and therefore undeclared) most of the time, but can come in
handy sometimes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The optional &lt;code&gt;context&lt;/code&gt; parameter is what the iterator function will be bound to. If used, the &lt;code&gt;this&lt;/code&gt; keyword inside the iterator will point to the object given by the argument.&lt;/p&gt;

&lt;h3&gt;&lt;code&gt;$break&lt;/code&gt; and &lt;code&gt;$continue&lt;/code&gt;&lt;/h3&gt;

&lt;p class=&quot;deprecated&quot;&gt;The usage of &lt;code&gt;$continue&lt;/code&gt; is deprecated. This feature will not be available in releases after Prototype 1.5 in favor of speed. Instead&amp;mdash;since your blocks are in fact functions&amp;mdash;&lt;strong&gt;simply issue a &lt;code&gt;return&lt;/code&gt; statement&lt;/strong&gt;. This will skip the rest of the block, jumping to the next iteration.&lt;/p&gt;

&lt;p&gt;Regular loops can be short-circuited in JavaScript using the &lt;code&gt;break&lt;/code&gt; and
&lt;code&gt;continue&lt;/code&gt; statements.  However, when using iterator functions, your code is
outside of the loop scope: the looping code happens behind the scene.&lt;/p&gt;

&lt;p&gt;In order to provide you with equivalent (albeit less optimal) functionality,
Prototype provides two global exception objects, &lt;code&gt;$break&lt;/code&gt; and &lt;code&gt;$continue&lt;/code&gt;.
Throwing these is equivalent to using the corresponding native statement in a
vanilla loop.  These exceptions are properly caught internally by the &lt;code&gt;each&lt;/code&gt;
method.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;['one', 'two', 'three'].each(function(s) {
  alert(s);
});

[ 'hello', 'world'].each(function(s, index) {
  alert(index + ': ' + s);
});
// alerts -&gt; '0: hello' then '1: world'

// This could be done better with an accumulator using inject, but humor me
// here...
var result = [];
$R(1,10).each(function(n) {
  if (0 == n % 2)
    throw $continue;
  if (n &gt; 6)
    throw $break;
  result.push(n);
});
// result -&gt; [1, 3, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;&lt;code&gt;each&lt;/code&gt; vs. &lt;code&gt;_each&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;If you read the &lt;a href=&quot;/api/enumerable&quot;&gt;main &lt;code&gt;Enumerable&lt;/code&gt; page&lt;/a&gt;, you may recall that
in order for a class to mix in &lt;code&gt;Enumerable&lt;/code&gt;, it has to provide the fundamental
looping code appropriate to its internal structure.  This basic iteration
method must be called &lt;code&gt;_each&lt;/code&gt;, and it only receives one argument: the iterator
function.  You&#8217;ll find further details on the main page.&lt;/p&gt;

&lt;p&gt;Basically, &lt;code&gt;Enumerable.each&lt;/code&gt; wraps the actual looping code provided by &lt;code&gt;_each&lt;/code&gt;
with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Support for break/continue, as described above.&lt;/li&gt;
&lt;li&gt;Proper maintenance and passing of the value/index arguments.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;Optimized version&lt;/h3&gt;

&lt;p&gt;There is a very common use-case that will probably be better taken care of by
a specialized variant: the method-calling scenario.&lt;/p&gt;

&lt;p&gt;Say you want to invoke the same method on all elements, possibly with
arguments.  You may or may not want to use the result values.  This can be
achieved easily with &lt;a href=&quot;invoke&quot;&gt;&lt;code&gt;invoke&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This variant usually performs better than &lt;code&gt;each&lt;/code&gt;, since it avoids lexical
closure costs.  However, it does accumulate the result values in an array,
which you might not need.  At any rate, you might want to benchmark both
options in your specific use case.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12348</id>
    <published>2006-10-29T19:15:00Z</published>
    <updated>2007-01-29T19:06:37Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/zip" rel="alternate" type="text/html"/>
    <title>zip</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;zip(Sequence...[, iterator = Prototype.K]) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;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.  Tuples can be converted to something else by applying the optional iterator on them.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;zip(Sequence...[, iterator = Prototype.K]) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;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.  Tuples can be converted to something else by applying the optional iterator on them.&lt;/p&gt;
&lt;p&gt;For those who never encountered a &lt;code&gt;zip&lt;/code&gt; function before (i.e. have not worked enough with languages such as Haskell or Ruby ;-)), the exact behavior of this method might be difficult to grasp.  Here are a few examples that should clear it up.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var firstNames = ['Justin', 'Mislav', 'Tobie', 'Christophe'];
var lastNames = ['Palmer', 'Marohnić', 'Langel', 'Porteneuve'];

firstNames.zip(lastNames)
// -&gt; [['Justin', 'Palmer'], ['Mislav', 'Marohnić'], ['Tobie', 'Langel'], ['Christophe', 'Porteneuve']]

firstNames.zip(lastNames, function(a) { return a.join(' '); })
// -&gt; ['Justin Palmer', 'Mislav Marohnić', 'Tobie Langel', 'Christophe Porteneuve']

var cities = ['Memphis', 'Zagreb', 'Montreal', 'Paris'];
firstNames.zip(lastNames, cities, function(p) {
  return p[0] + ' ' + p[1] + ', ' + p[2];
})
// -&gt; ['Justin Palmer, Memphis', 'Mislav Marohnić, Zagreb', 'Tobie Langel, Montreal', 'Christophe Porteneuve, Paris']

firstNames.zip($R(1, 100), function(a) { return a.reverse().join('. '); })
// -&gt; ['1. Justin', '2. Mislav', '3. Tobie', '4. Christophe']
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12347</id>
    <published>2006-10-29T19:14:00Z</published>
    <updated>2007-01-06T14:06:06Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/toArray" rel="alternate" type="text/html"/>
    <title>toArray</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;toArray() -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns an &lt;code&gt;Array&lt;/code&gt; representation of the enumeration. Aliased as &lt;a href=&quot;/api/enumerable/entries&quot;&gt;&lt;code&gt;entries&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;toArray() -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns an &lt;code&gt;Array&lt;/code&gt; representation of the enumeration. Aliased as &lt;a href=&quot;/api/enumerable/entries&quot;&gt;&lt;code&gt;entries&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Note: this makes any object that mixes in &lt;code&gt;Enumerable&lt;/code&gt; amenable to the &lt;a href=&quot;/api/utility/dollar-a&quot;&gt;&lt;code&gt;$A&lt;/code&gt;&lt;/a&gt; utility function.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$R(1, 5).toArray()
// -&gt; [1, 2, 3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Performance considerations&lt;/h3&gt;

&lt;p&gt;Obviously, objects that mix in &lt;code&gt;Enumerable&lt;/code&gt; may override the default code, as &lt;a href=&quot;/api/array/toArray&quot;&gt;&lt;code&gt;Array&lt;/code&gt; does&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12346</id>
    <published>2006-10-29T19:13:00Z</published>
    <updated>2007-11-15T17:04:45Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/sortBy" rel="alternate" type="text/html"/>
    <title>sortBy</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;sortBy(iterator[, context]) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Provides a custom-sorted view of the elements based on the criteria computed, for each element, by the iterator.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;sortBy(iterator[, context]) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Provides a custom-sorted view of the elements based on the criteria computed, for each element, by the iterator.&lt;/p&gt;
&lt;p&gt;Elements of equivalent criterion value are left in existing order.  Computed criteria must have well-defined strict weak ordering semantics (i.e. the &lt;code&gt;&amp;lt;&lt;/code&gt; operator must exist between any two criteria).&lt;/p&gt;

&lt;p&gt;Note that arrays already feature a native &lt;code&gt;sort&lt;/code&gt; method, which relies on &lt;em&gt;natural ordering&lt;/em&gt; of the array's elements (i.e. the semantics of the &lt;code&gt;&amp;lt;&lt;/code&gt; operator when applied to two such elements).  You should use &lt;code&gt;sortBy&lt;/code&gt; only whe natural ordering is nonexistent or otherwise unsatisfactory.&lt;/p&gt;

&lt;p&gt;The optional &lt;code&gt;context&lt;/code&gt; parameter is what the iterator function will be bound to. If used, the &lt;code&gt;this&lt;/code&gt; keyword inside the iterator will point to the object given by the argument.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
['hello', 'world', 'this', 'is', 'nice'].sortBy(function(s) { return s.length; })
// -&gt; 'is', 'this', 'nice', 'hello', 'world']

['hello', 'world', 'this', 'is', 'cool'].sortBy(function(s) {
  var md = s.match(/[aeiouy]/g);
  return null == md ? 0 : md.length;
})
// -&gt; [ 'world', 'this', 'is', 'hello', 'cool'] (sorted by vowel count)
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12344</id>
    <published>2006-10-29T19:12:00Z</published>
    <updated>2007-11-15T17:04:28Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/reject" rel="alternate" type="text/html"/>
    <title>reject</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;reject(iterator[, context]) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns all the elements for which the iterator returned &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;reject(iterator[, context]) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns all the elements for which the iterator returned &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The optional &lt;code&gt;context&lt;/code&gt; parameter is what the iterator function will be bound to. If used, the &lt;code&gt;this&lt;/code&gt; keyword inside the iterator will point to the object given by the argument.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$R(1, 10).reject(function(n) { return 0 == n % 2; })
// -&gt; [1, 3, 5, 7, 9]

[ 'hello', 'world', 'this', 'is', 'nice'].reject(function(s) {
  return s.length &gt;= 5;
})
// -&gt; ['this', 'is', 'nice']
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;findAll&quot;&gt;&lt;code&gt;findAll&lt;/code&gt;&lt;/a&gt; method (and its &lt;a href=&quot;select&quot;&gt;&lt;code&gt;select&lt;/code&gt;&lt;/a&gt; alias) are the opposites of this one.  If you need to split elements in two groups based upon a predicate, look at &lt;a href=&quot;partition&quot;&gt;&lt;code&gt;partition&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12345</id>
    <published>2006-10-29T19:12:00Z</published>
    <updated>2007-01-04T22:10:21Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/select" rel="alternate" type="text/html"/>
    <title>select</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;select(iterator) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Alias for the &lt;a href=&quot;/api/enumerable/findAll&quot;&gt;&lt;code&gt;findAll&lt;/code&gt;&lt;/a&gt; method.&lt;/p&gt;</summary>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12343</id>
    <published>2006-10-29T19:11:00Z</published>
    <updated>2008-06-09T19:43:27Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/pluck" rel="alternate" type="text/html"/>
    <title>pluck</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;pluck(propertyName) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Optimization for a common use-case of &lt;a href=&quot;/api/enumerable/collect&quot;&gt;&lt;code&gt;collect&lt;/code&gt;&lt;/a&gt;: fetching the same property for all the elements.  Returns the property values.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;pluck(propertyName) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Optimization for a common use-case of &lt;a href=&quot;/api/enumerable/collect&quot;&gt;&lt;code&gt;collect&lt;/code&gt;&lt;/a&gt;: fetching the same property for all the elements.  Returns the property values.&lt;/p&gt;
&lt;p&gt;Since it avoids the cost of a lexical closure over an anonymous function (like
you would do with &lt;a href=&quot;collect&quot;&gt;&lt;code&gt;collect&lt;/code&gt;&lt;/a&gt;), this performs much better.&lt;/p&gt;

&lt;p&gt;Perhaps more importantly, it definitely makes for more concise &lt;em&gt;and&lt;/em&gt; more
readable source code.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
['hello', 'world', 'this', 'is', 'nice'].pluck('length')
// -&gt; [5, 5, 4, 2, 4]

document.getElementsByClassName('superfluous').pluck('tagName').sort().uniq()
// -&gt; sorted list of unique canonical tag names for elements with this
// specific CSS class...
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;See also&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;invoke&quot;&gt;&lt;code&gt;invoke&lt;/code&gt;&lt;/a&gt; method does much the same thing for method invoking.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12342</id>
    <published>2006-10-29T19:10:00Z</published>
    <updated>2007-11-15T17:04:13Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/partition" rel="alternate" type="text/html"/>
    <title>partition</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;partition([iterator = Prototype.K[, context]]) -&gt; [TrueArray, FalseArray]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Partitions the elements in two groups: those regarded as &lt;code&gt;true&lt;/code&gt;, and those considered &lt;code&gt;false&lt;/code&gt;.  By default, regular JavaScript boolean equivalence is used, but an iterator can be provided, that computes a boolean representation of the elements.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;partition([iterator = Prototype.K[, context]]) -&gt; [TrueArray, FalseArray]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Partitions the elements in two groups: those regarded as &lt;code&gt;true&lt;/code&gt;, and those considered &lt;code&gt;false&lt;/code&gt;.  By default, regular JavaScript boolean equivalence is used, but an iterator can be provided, that computes a boolean representation of the elements.&lt;/p&gt;
&lt;p&gt;This is a preferred solution to using both &lt;a href=&quot;findAll&quot;&gt;&lt;code&gt;findAll&lt;/code&gt;&lt;/a&gt;/&lt;code&gt;select&lt;/code&gt; and
&lt;a href=&quot;reject&quot;&gt;&lt;code&gt;reject&lt;/code&gt;&lt;/a&gt;: it only loops through the elements once!&lt;/p&gt;

&lt;p&gt;The optional &lt;code&gt;context&lt;/code&gt; parameter is what the iterator function will be bound to. If used, the &lt;code&gt;this&lt;/code&gt; keyword inside the iterator will point to the object given by the argument.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;['hello', null, 42, false, true, , 17].partition()
// -&gt; [['hello', 42, true, 17], [null, false, undefined]]

$R(1, 10).partition(function(n) {
  return 0 == n % 2;
})
// -&gt; [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12340</id>
    <published>2006-10-29T19:07:00Z</published>
    <updated>2007-01-06T13:46:13Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/member" rel="alternate" type="text/html"/>
    <title>member</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;member(object) -&gt; Boolean&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Determines whether a given object is in the &lt;code&gt;Enumerable&lt;/code&gt; or not, based on the &lt;code&gt;==&lt;/code&gt; comparison operator.  Convenience alias for &lt;a href=&quot;/api/enumerable/include&quot;&gt;&lt;code&gt;include&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;</summary>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12341</id>
    <published>2006-10-29T19:07:00Z</published>
    <updated>2007-11-15T17:03:11Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/min" rel="alternate" type="text/html"/>
    <title>min</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;min([iterator = Prototype.K[, context]]) -&gt; minValue&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the minimum element (or element-based computation), or &lt;code&gt;undefined&lt;/code&gt; if the enumeration is empty.  Elements are either compared directly, or by first applying the iterator and comparing returned values.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;min([iterator = Prototype.K[, context]]) -&gt; minValue&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the minimum element (or element-based computation), or &lt;code&gt;undefined&lt;/code&gt; if the enumeration is empty.  Elements are either compared directly, or by first applying the iterator and comparing returned values.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: for equivalent elements, the &lt;strong&gt;earliest&lt;/strong&gt; one is returned.&lt;/p&gt;

&lt;p&gt;The optional &lt;code&gt;context&lt;/code&gt; parameter is what the iterator function will be bound to. If used, the &lt;code&gt;this&lt;/code&gt; keyword inside the iterator will point to the object given by the argument.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$R(1,10).min()
// -&gt; 1

['hello', 'world', 'gizmo'].min()
// -&gt; 'gizmo'

function Person(name, age) {
   this.name = name;
   this.age = age;
}

var john = new Person('John', 20);
var mark = new Person('Mark', 35);
var daisy = new Person('Daisy', 22);

[john, mark, daisy].min(function(person) {
  return person.age;
})
// -&gt; 20
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12339</id>
    <published>2006-10-29T19:06:00Z</published>
    <updated>2007-01-04T22:14:15Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/map" rel="alternate" type="text/html"/>
    <title>map</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;map(iterator) -&gt; Array&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the results of applying the iterator to each element. Convenience alias for &lt;a href=&quot;/api/enumerable/collect&quot;&gt;&lt;code&gt;collect&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;</summary>  </entry>
  <entry xml:base="http://prototypejs.org:80/">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org:80,2006-10-29:12338</id>
    <published>2006-10-29T19:02:00Z</published>
    <updated>2007-11-15T17:02:54Z</updated>
    <category term="Enumerable"/>
    <link href="http://prototypejs.org:80/api/enumerable/max" rel="alternate" type="text/html"/>
    <title>max</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;max([iterator = Prototype.K[, context]]) -&gt; maxValue&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the maximum element (or element-based computation), or &lt;code&gt;undefined&lt;/code&gt; if the enumeration is empty.  Elements are either compared directly, or by first applying the iterator and comparing returned values.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;max([iterator = Prototype.K[, context]]) -&gt; maxValue&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Returns the maximum element (or element-based computation), or &lt;code&gt;undefined&lt;/code&gt; if the enumeration is empty.  Elements are either compared directly, or by first applying the iterator and comparing returned values.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: for equivalent elements, the &lt;strong&gt;latest&lt;/strong&gt; one is returned.&lt;/p&gt;

&lt;p&gt;The optional &lt;code&gt;context&lt;/code&gt; parameter is what the iterator function will be bound to. If used, the &lt;code&gt;this&lt;/code&gt; keyword inside the iterator will point to the object given by the argument.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
$R(1,10).max()
// -&gt; 10

['hello', 'world', 'gizmo'].max()
// -&gt; 'world'

function Person(name, age) {
   this.name = name;
   this.age = age;
}

var john = new Person('John', 20);
var mark = new Person('Mark', 35);
var daisy = new Person('Daisy', 22);

[john, mark, daisy].max(function(person) {
  return person.age;
})
// -&gt; 35
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
</feed>

