LoginSignup
0
0

More than 1 year has passed since last update.

【Google App Script】配列内の要素を検索してインデックス番号を取得する方法

Last updated at Posted at 2022-08-16

GoogleAppScriptにおける、配列の検索方法について記載します。
配列オブジェクトに対して、indexOf()メソッドを使います。

配列の先頭から検索していき、最初に見つかったインデックス番号(0始まり)を返します。
見つからなかった場合は、-1を返します。

index_of.gs
// 変数arrayに配列を定義する
array = ["りんご", "みかん", "いちご", "りんご"];

// 配列に対してindexOf()メソッドを適用
console.log(array.indexOf("りんご")); // -> 0
console.log(array.indexOf("みかん")); // -> 1
console.log(array.indexOf("いちご")); // -> 2
console.log(array.indexOf("ぶどう")); // -> -1

参考

0
0
0

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