LoginSignup
1
0

More than 5 years have passed since last update.

日〜土の入った配列を「○曜日はじまり」で並び替え

Last updated at Posted at 2016-12-18

やりたいこと

const weekday = [
  '',
  '',
  '',
  '',
  '',
  '',
  ''
];

こういう配列を、月曜始まりとか火曜始まりとかで並び替えたい

const start = '';

const result = [
  ...[...weekday].splice(weekday.indexOf(start)),
  ...[...weekday].splice(0, weekday.indexOf(start))
];

console.log(result)
//=> ["土", "日", "月", "火", "水", "木", "金"]

spliceが破壊的なせいで、サクッと書けないが、spread and rest syntaxがあるので、少しは楽

識者乞 or ぼちぼち調べる

  • 曜日とか、時(1〜24)、秒・分(1〜60)とかのような末尾までいったら先頭に戻るようなデータの構造を表現した名前
  • こういうデータの順番を変える操作を表現した名前

(追記)circular shift

ここでやりたかった操作はcircular shiftと呼ぶことがわかった

(追記) rotate-array

rotate-array

テストのあるnpm pkgあったので、いまはオレオレコード捨ててこれを使ってる

1
0
3

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
1
0