Prototype-array-indexof

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

プロトタイプ-indexOf()メソッド

このメソッドは、配列内で引数が最初に現れる位置を返します。 引数が配列に存在しない場合、-1を返します。

-配列の最初のインデックスはゼロから始まります。

構文

array.indexOf(value);

戻り値

配列内で引数が最初に現れる位置を返します。 引数が配列に存在しない場合、-1を返します。

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

      <script>
         function showResult() {
            var arr = [3, 5, 6, 1, 20];
            alert("Found position : "  + arr.indexOf(6) );
         }
      </script>
   </head>

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

出力