LoginSignup
2
0

More than 3 years have passed since last update.

【JavaScript】誕生年の配列を作るサンプルコード(ギネス世界記録を反映)

Posted at

はじめに

ギネス世界記録の最高齢を反映した、誕生年の整数配列を作るサンプルコードを残します。
セレクトボックスで使えるのではと思います。

環境

OS: macOS Catalina 10.15.3

結論

sample.js
  const year = new Date().getFullYear()

  // ギネス世界記録の最高齢
  const maxAge = 117 

  const start = year - maxAge
  const end = year

  // 必要な範囲の整数配列を作成
  const years = [...Array(end - start + 1).keys()].map( i => start + i)

  // 降順に並び替えて選びやすくする
  // ※最終的にはこれを使う
  const descYears = years.sort((a,b) => {
    return (a < b ? 1 : -1)
  })

おわりに

最後まで読んで頂きありがとうございました:bow_tone1:

どなたかの参考になれば幸いです:relaxed:

2
0
0

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
0