1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScript 連番数値配列([0,1,2]や[3,5,7]や[-5,-4,-3])作成

Last updated at Posted at 2020-05-20

コード


const createArray = (start, end, increment = 1) => {
  if (increment === 0) {
    throw new Error(`Error createArray args increment is 0`);
  }
  const result = [];
  if (start <= end) {
    for (let i = start, l = end; i <= l; i += increment) {
      result.push(i);
    }
  } else {
    for (let i = start, l = end; i >= l; i += increment) {
      result.push(i);
    }
  }
  return result;
}

// Operation check
console.log(createArray(0,4,1));    // [ 0, 1, 2, 3, 4 ]
console.log(createArray(0,4,2));    // [ 0, 2, 4 ]
console.log(createArray(2,4,1));    // [ 2, 3, 4 ]
console.log(createArray(2,5,2));    // [ 2, 4 ]
console.log(createArray(2,6,2));    // [ 2, 4, 6 ]
console.log(createArray(2,10,3));   // [ 2, 5, 8 ]
console.log(createArray(2,11,3));   // [ 2, 5, 8, 11 ]
console.log(createArray(2,12,3));   // [ 2, 5, 8, 11 ]
console.log(createArray(2,13,3));   // [ 2, 5, 8, 11 ]
console.log(createArray(2,14,3));   // [ 2, 5, 8, 11, 14 ]
console.log(createArray(14,2,-3));  // [ 14, 11, 8, 5, 2 ]
console.log(createArray(13,2,-3));  // [ 13, 10, 7, 4 ]
console.log(createArray(12,2,-3));  // [ 12, 9, 6, 3 ]
console.log(createArray(11,2,-3));  // [ 11, 8, 5, 2 ]

console.log(createArray(0,0,1));  // [ 0 ]
console.log(createArray(0,0,2));  // [ 0 ]
console.log(createArray(1,1,1));  // [ 1 ]
console.log(createArray(1,1,2));  // [ 1 ]
console.log(createArray(-4,1,2)); // [ -4, -2, 0 ]
console.log(createArray(-4,0,1)); // [ -4, -3, -2, -1, 0 ]

参考

連番の数字の配列を作成(es2015 ver) - Qiita
https://qiita.com/sakymark/items/710f0b9a632c375fbc31

JavaScriptで[ 0, 1, 2, 3, 4 ]のような連番の配列を生成する方法 - Qiita
https://qiita.com/suin/items/1b39ce57dd660f12f34b

JavaScriptで[ 0, 1, 2, 3, 4 ]のような連番の配列を生成する僕のやり方 - Qiita
https://qiita.com/leak4mk0/items/38529321f797ef8a2bd3

jsでrange関数をつくる - Qiita
https://qiita.com/akameco/items/a2b698dd4a067754997b

JavaScriptでrange関数作ってみた - Qiita
https://qiita.com/RyutaKojima/items/168632d4980e65a285f3

Javascriptでrange()みたいに指定長の配列を生成 - 動かざることバグの如し
https://thr3a.hatenablog.com/entry/20190413/1555134381

指定した範囲の整数配列の作り方 in JavaScript ES6 - 無駄と文化
https://blog.mudatobunka.org/entry/2015/10/31/222750

感想

検索しても Array.from とか、スプレッド構文とか、map とか可読性低いパズルプログラミングを追求してしまう人多い。これじゃあ、初心者が変なJavaScriptを覚えてしまうわけだ。

開始終了の変更とかincrement値の変更とか、start end の逆とか、0値のエラー排除とかの対応、パズル構文だと難しくて対応できない。そういえば整数だけじゃなくて実数もあった。(たぶん上記コードは対応してると思う)

実用的なのはパズル記述ではなく、コード可読性が高く変更容易性が高く機能が豊富だったりとかそういう部品であって、内部実装なんてどうでもいいことです。

1
3
4

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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?