このロケールの翻訳が存在しないため、英語バージョンのコンテンツを表示しています。 Help us translate this article!
配列の要素を In-place アルゴリズム で反転させます。最初の要素は最後に、最後の要素は最初になります。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
a.reverse()
戻り値
反転された配列です【訳注: a
が直接反転され、返されます】。
説明
reverse
メソッドは呼び出した配列オブジェクトの要素をその内部で反転させ、配列を書き換えます。そして、書き換えられた配列を戻します。
reverse
は意図的に汎用です。このメソッドは can be called or applied to objects resembling arrays. Objects which do not contain a length
property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.
例
配列の要素を反転させる
次の例では、3 つの要素を持つ配列 a
を作り、反転させます。reverse()
は要素が反転された a
の参照を返します。
const a = [1, 2, 3]; console.log(a); // [1, 2, 3] a.reverse(); console.log(a); // [3, 2, 1]
配列風オブジェクトの要素を反転する
The following example creates an array-like object a
, containing three elements and a length property, then reverses the array-like object. The call to reverse()
returns a reference to the reversed array-like object a
.
const a = {0: 1, 1: 2, 2: 3, length: 3}; console.log(a); // {0: 1, 1: 2, 2: 3, length: 3} Array.prototype.reverse.call(a); //same syntax for using apply() console.log(a); // {0: 3, 1: 2, 2: 1, length: 3}
仕様
仕様書 | 策定状況 | コメント |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | 標準 | 初期定義です。JavaScript 1.1 で実装されました。 |
ECMAScript 5.1 (ECMA-262) Array.prototype.reverse の定義 |
標準 | |
ECMAScript 2015 (6th Edition, ECMA-262) Array.prototype.reverse の定義 |
標準 | |
ECMAScript Latest Draft (ECMA-262) Array.prototype.reverse の定義 |
ドラフト |
ブラウザー実装状況
デスクトップ | モバイル | サーバー | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
reverse | Chrome 完全対応 1 | Edge 完全対応 12 | Firefox 完全対応 1 | IE 完全対応 5.5 | Opera 完全対応 あり | Safari 完全対応 あり | WebView Android 完全対応 あり | Chrome Android 完全対応 18 | Firefox Android 完全対応 4 | Opera Android 完全対応 あり | Safari iOS 完全対応 あり | Samsung Internet Android 完全対応 あり | nodejs 完全対応 あり |
凡例
- 完全対応
- 完全対応