2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

配列を「回転」させる

Last updated at Posted at 2024-10-18

[ 0, 1, 2, 3, 4 ][ 3, 4, 0, 1, 2 ] みたいなことをやりたいが、すぐ忘れるのでメモ。

    let array = [ 0, 1, 2, 3, 4 ];
    array = array.splice(3).concat(array);

これで array が [ 3, 4, 0, 1, 2 ] に変更される。再代入を忘れずに。

array.splice(3) は array から添字 3 以降の要素を取り除いて返す。つまり、[ 3, 4 ].concat([ 0, 1, 2])[ 3, 4, 0, 1, 2 ] となる寸法。

2
2
2

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?