【JavaScript】lastIndexOf()の使い方: 配列で指定した要素の最後の位置を見つける方法
![【JavaScript】lastIndexOf()の使い方: 配列で指定した要素の最後の位置を見つける方法](https://images.microcms-assets.io/assets/a4ac2815023448e4b3b4f16c7ac79a5c/8bc60cc341aa4da6863c70460bc8d062/eyecatch-3.jpg?w=1200&h=630&q=80&mark=https://images.microcms-assets.io/assets/a4ac2815023448e4b3b4f16c7ac79a5c/10d6abf043a04857b2a21be2f66a0513/NU_LOGiC.png&mark-w=168&mark-align=bottom&mark-pad=156&blend-align=center,middle&blend-mode=normal&blend64=aHR0cHM6Ly9pbWFnZXMubWljcm9jbXMtYXNzZXRzLmlvL350ZXh0P3R4dHNpemU9NDImdz05MDAmaD00NzImdHh0LWFsaWduPWNlbnRlcixtaWRkbGUmdHh0Zm9udD1IaXJhZ2lubyUyMFNhbnMlMjBXNiZ0eHQtY29sb3I9MDAwJnR4dDY0PUlPT0FrRXBoZG1GVFkzSnBjSFRqZ0pGc1lYTjBTVzVrWlhoUFppZ3A0NEd1NUwyXzQ0R0U1cGE1T2lEcGhZM2xpSmZqZ2FmbWpJZmxycHJqZ1pmamdaX29wb0hudEtEamdhN21uSURsdm96amdhN2t2WTNudmE3amdwTG9wb3ZqZ2FUamdaSGpnb3ZtbHJubXM1VQ)
JavaScriptのlastIndexOf()
メソッドは、配列や文字列内で指定した要素が最後に出現する位置(インデックス)を見つけるために使用されます。この記事では、lastIndexOf()
の使い方や、実用的な例、さらには注意点について解説します。
3行で要約すると
lastIndexOf()
は配列や文字列で指定した要素の最後に出現する位置を見つけます。- このメソッドは条件を自分で設定するためのコールバック関数を必要としません。
- 数々の実用的な例や類似するメソッドとの比較も網羅しています。
「lastIndexOf()」の基本的な使い方
「lastIndexOf()」とは?
lastIndexOf()
メソッドは、指定した要素が最後に出現する位置(インデックス)を返すメソッドです。もし要素が見つからない場合は、-1
を返します。
例を見てみましょう。
const fruits = ['apple', 'banana', 'cherry', 'apple'];
const index = fruits.lastIndexOf('apple');
console.log(index); // 3
この例では、「apple」が最後に出現する位置(インデックス)は3
です。
「lastIndexOf()」の使用例4パターン
文字列で特定の文字が最後に現れる位置を見つける
const greeting = 'Hello, world! Have a good world!';
const index = greeting.lastIndexOf('world');
console.log(index); // 27
'world'が最後に現れる位置はインデックス27
です。
配列で特定の数値が最後に出る位置を確認する
const numbers = [1, 2, 3, 4, 3, 5];
const index = numbers.lastIndexOf(3);
console.log(index); // 4
数字3
が最後に出る位置はインデックス4
です。
オブジェクトの配列で特定のプロパティ値が最後に出る位置を確認する
const persons = [{name: 'Alice'}, {name: 'Bob'}, {name: 'Alice'}];
const index = persons.map(person => person.name).lastIndexOf('Alice');
console.log(index); // 2
'Alice'という名前が最後に出る位置はインデックス2
です。
検索開始位置を指定する
const numbers = [1, 2, 3, 4, 3, 5];
const index = numbers.lastIndexOf(3, 3);
console.log(index); // 2
数字3
をインデックス3
から左に向かって検索を開始し、最初に出る位置はインデックス2
です。
「lastIndexOf()」と「indexOf()」
「indexOf()」とは?
indexOf()
も要素が最初に出現する位置を探すメソッドです。
const numbers = [1, 2, 3, 4, 3, 5];
const index = numbers.indexOf(3);
console.log(index); // 2
「lastIndexOf()」と「indexOf()」の違い
lastIndexOf()
は要素が最後に出現する位置を、indexOf()
は要素が最初に出現する位置を返します。
// lastIndexOf()
const lastIndex = [1, 2, 3, 4, 3, 5].lastIndexOf(3);
console.log(lastIndex); // 4
// indexOf()
const firstIndex = [1, 2, 3, 4, 3, 5].indexOf(3);
console.log(firstIndex); // 2
「lastIndexOf()」を使用する際の注意点
要素が見つからない場合の戻り値
lastIndexOf()
メソッドが要素を見つけられなかった場合、-1
を返します。この点を注意深く扱いましょう。
const fruits = ['apple', 'banana', 'cherry'];
const index = fruits.lastIndexOf('pear');
console.log(index); // -1
「lastIndexOf()」の使い方まとめ
lastIndexOf()
は配列や文字列で、指定した要素が最後に出現する位置を見つけるためのメソッドです。- コールバック関数は必要ありません。
indexOf()
とは違い、要素が最後に出現する位置を見つけます。- 要素が存在しない場合、
-1
が返されるので注意が必要です。
これで、lastIndexOf()
の使い方から実用的な例、注意点までしっかりと理解できたと思います。ぜひ実際のコードで使ってみてください。