6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptで配列を一時的に逆順にしたい場合は slice().reverse()

Last updated at Posted at 2019-10-12

JavaScriptで配列を逆順にするには、reverse() メソッドを使います。

var table = ['a', 'b', 'c'];
console.log(table.reverse());

しかし、このメソッドは配列自体を加工してしまうため、それ以降配列が常に逆順になってしまいます。

表示するときだけ逆順にしたいなどの場合は、別の配列に移してもよいですが次のようにすると手軽です。

console.log(table.slice().reverse());
6
5
1

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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?