Array.prototype.reverse()

Array 实例的 reverse() 方法反转数组 到位 并返回对同一数组的引用,第一个数组元素现在成为最后一个,最后一个数组元素成为第一个。换句话说,数组中的元素顺序将转向与前面所述相反的方向。

¥The reverse() method of Array instances reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.

要反转数组中的元素而不改变原始数组,请使用 toReversed()

¥To reverse the elements in an array without mutating the original array, use toReversed().

Try it

语法

¥Syntax

js
reverse()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

对原始数组的引用,现在已反转。请注意,该数组已反转 到位,并且没有进行任何复制。

¥The reference to the original array, now reversed. Note that the array is reversed in place, and no copy is made.

描述

¥Description

reverse() 方法将调用数组对象的元素转置到位,改变数组,并返回对该数组的引用。

¥The reverse() method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.

reverse() 方法保留空槽。如果源数组为 sparse,则空槽对应的新索引为 deleted,也成为空槽。

¥The reverse() method preserves empty slots. If the source array is sparse, the empty slots' corresponding new indices are deleted and also become empty slots.

reverse() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。虽然字符串也是类似数组的,但此方法不适合应用于它们,因为字符串是不可变的。

¥The reverse() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

示例

¥Examples

反转数组中的元素

¥Reversing the elements in an array

以下示例创建一个包含三个元素的数组 items,然后反转该数组。对 reverse() 的调用返回对反转数组 items 的引用。

¥The following example creates an array items, containing three elements, then reverses the array. The call to reverse() returns a reference to the reversed array items.

js
const items = [1, 2, 3];
console.log(items); // [1, 2, 3]

items.reverse();
console.log(items); // [3, 2, 1]

verse() 方法返回对同一数组的引用

¥The reverse() method returns the reference to the same array

reverse() 方法返回对原始数组的引用,因此改变返回的数组也会改变原始数组。

¥The reverse() method returns reference to the original array, so mutating the returned array will mutate the original array as well.

js
const numbers = [3, 2, 4, 1, 5];
const reversed = numbers.reverse();
// numbers and reversed are both in reversed order [5, 1, 4, 2, 3]
reversed[0] = 5;
console.log(numbers[0]); // 5

如果你希望 reverse() 不改变原始数组,而是像其他数组方法(例如 map())一样返回 shallow-copied 数组,请使用 toReversed() 方法。或者,你可以在调用 reverse() 之前使用 扩展语法Array.from() 执行浅复制。

¥In case you want reverse() to not mutate the original array, but return a shallow-copied array like other array methods (e.g. map()) do, use the toReversed() method. Alternatively, you can do a shallow copy before calling reverse(), using the spread syntax or Array.from().

js
const numbers = [3, 2, 4, 1, 5];
// [...numbers] creates a shallow copy, so reverse() does not mutate the original
const reverted = [...numbers].reverse();
reverted[0] = 5;
console.log(numbers[0]); // 3

在稀疏数组上使用 reverse()

¥Using reverse() on sparse arrays

稀疏数组在调用 reverse() 后仍保持稀疏状态。空槽被复制到它们各自的新索引作为空槽。

¥Sparse arrays remain sparse after calling reverse(). Empty slots are copied over to their respective new indices as empty slots.

js
console.log([1, , 3].reverse()); // [3, empty, 1]
console.log([1, , 3, 4].reverse()); // [4, 3, empty, 1]

对非数组对象调用 reverse()

¥Calling reverse() on non-array objects

reverse() 方法读取 thislength 属性。然后,它访问具有 0length / 2 之间整数键的每个属性,并交换两端的两个相应索引,deleting 是源属性不存在的任何目标属性。

¥The reverse() method reads the length property of this. It then visits each property having an integer key between 0 and length / 2, and swaps the two corresponding indices on both ends, deleting any destination property for which the source property did not exist.

js
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
  3: 33, // ignored by reverse() since length is 3
};
console.log(Array.prototype.reverse.call(arrayLike));
// { 0: 4, 3: 33, length: 3, unrelated: 'foo' }
// The index 2 is deleted because there was no index 0 present originally
// The index 3 is unchanged since the length is 3

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.reverse

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看