Prototype-enum-eachslice

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

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

このメソッドは、指定されたサイズに基づいてアイテムをチャンクにグループ化します。最後のチャンクは小さい可能性があります。

オプションのコンテキストパラメータは、イテレータ関数がバインドされるものです。 使用する場合、イテレータ内の_this_キーワードは、引数で指定されたオブジェクトを指します。

構文

Iterator.eachSlice([context]);

戻り値

スライスの配列(array)を返します。

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>

      <script>
         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 }
         ];

         function showResult() {
            alert ( students.eachSlice(4, function(toon) {
               return toon.pluck('name');
            }) );
            var arr = students.eachSlice(2).first();
               arr.each(function(s) {
               var h = $H(s);
               alert("Value : " + h.inspect());
            });
         }
      </script>
   </head>

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

出力