Prototype-enum-grep

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

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

このメソッドは、フィルターに一致するすべての要素を返します。 反復子が提供されている場合は、選択された各要素の戻り値を生成するために使用されます。

オプションのiteratorパラメーターは、map()メソッドと同様の方法で結果セットを変換します。

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

構文

Iterator.findAll();

戻り値

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

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

      <script>
         function showResult() {
           //Get all strings with a repeated letter somewhere

            alert(['hello', 'world', 'is', 'cool'].grep(/(.)\1/).inspect());
           //Returns ['hello', 'cool']

           //Get all numbers ending with 0 or 5
            alert($R(1,30).grep(/[05]$/).inspect() );
           //Returns [5, 10, 15, 20, 25, 30]
         }
      </script>
   </head>

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

出力