Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

JavaScript で n を与えて 1からn までの配列を作る

More than 3 years have passed since last update.

Ruby の 1...4 みたいなことがしたくて、JavaScript で作る場合どうするか考えてみた。

> Array.from({length: 4}, (_, i) => i+1)
[ 1, 2, 3, 4 ]

ちなみに下記でもできる(バージョンにより挙動が不安定なので非推奨)

> [...new Array(4)].map( (_,i) => i );
[ 0, 1, 2, 3 ]
> new Array(4).map( (_,i) => i );
[ , , , ,  ]

参考

Array.from()のリファレンス
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/from

seteen
Angularを広めたいエンジニア Ruby, Angularが好きです。
https://twitter.com/yazumoto
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away