7
2

More than 5 years have passed since last update.

ES6で指定範囲の整数配列を作る

Posted at

Pythonのrange関数っぽく、指定した範囲の整数の配列を作る方法。

> Array.from(Array(10).keys())

[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

1から10にしたければ、

> Array.from(Array(10).keys(), x => x + 1)

[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

0始まりの配列ならこういう書き方もある。

> [...Array(10).keys()]

[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
7
2
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
7
2