Prototype - Enumerable inGroupsOf() Method



This method groups the items in fixed-size chunks, using a specific value to fill up the last chunk if necessary.

Syntax

Iterator.inGroupsOf(size[, filler = null]);

Return Value

Returns all the elements for which the iterator returned true.

Example

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            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 }
            ];

            alert ( students.pluck('name').inGroupsOf(4)  ) ;
            // Returns  [ ['Sunny', 'Audrey', 'Matt', 'Lodie'],
            //      ['Will', 'David', 'Julien', 'Thomas'],
            //      ['Serpil', null, null, null] ]
         }
      </script>
   </head>

   <body>
      <p>Click the button to see the result.</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

Output

prototype_enumerating.htm
Advertisements