Prototype-enum-ingroupsof

提供:Dev Guides
移動先:案内検索

プロトタイプ-列挙可能なinGroupsOf()メソッド

このメソッドは、必要に応じて特定の値を使用して最後のチャンクを埋める固定サイズのチャンクにアイテムをグループ化します。

構文

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

戻り値

反復子がtrueを返したすべての要素を返します。

<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>

出力