LoginSignup
141
118

More than 5 years have passed since last update.

Javascriptで配列内に特定の要素が存在するかどうかの判定

Last updated at Posted at 2015-04-13

JSの配列にはexistsとかcontainsなどとかいうメソッドは無いがindexOfを利用して以下のように判定できる。

var arr = ["a", "b", "c"];

if (arr.indexOf("a") >= 0){
  // 存在する
}

if (arr.indexOf("a") == -1){
  // 存在しない
}

参考

Javascriptで配列にある値が存在するかどうかを確認する方法について - 主にプログラムを勉強するブログ
http://d.hatena.ne.jp/artgear/20130124/javascript_array_contain_how_to

141
118
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
141
118