reverse()
metodu dizi elemanlarını tersten sıralar . Dizinin ilk elemanı son eleman olur, son elemanı ilk eleman olur.
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.
Syntax
a.reverse()
Dönen Değer
Ters dizi.
Açıklama
reverse
metodu dizinin olduğu halini kalıcı olarak değiştirir ve değiştirilmiş diziyi döndürür.
reverse
bilinçli olarak generic metodtur; bu şekilde dizilere benzeyen nesneler çağrılabilir veya uygulanabilir. Bir dizi ardışık, sıfır tabanlı sayısal özellikte sonuncuyu yansıtan length
özelliği içermeyen nesneler anlamlı bir şekilde davranmayabilir.
Örnekler
Bir dizideki öğeleri tersine çevirme
Aşağıdaki örnek, üç öğe içeren bir a
dizisi oluşturur, ardından diziyi tersine çevirir. reverse()
çağrısı, ters çevrilmiş a
dizisine bir referans döndürür.
const a = [1, 2, 3];
console.log(a); // [1, 2, 3]
a.reverse();
console.log(a); // [3, 2, 1]
Dizi benzeri bir nesnedeki öğeleri tersine çevirme
Aşağıdaki örnek, üç öğe ve length özelliği içeren dizi benzeri bir nesne a
oluşturur, sonra dizi benzeri nesneyi tersine çevirir. reverse ()
çağrısı, ters dizi benzeri nesneye a
bir referans döndürür.
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}
Özellikler
Özellik | Durum | Yorum |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.reverse' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.reverse' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'Array.prototype.reverse' in that specification. |
Living Standard |
Tarayıcı Uyumluluğu
BCD tables only load in the browser