LoginSignup
1
0

【初心者】fillメソッドの使い方

Posted at

fillメソッドとは

fill()メソッドがわからなかったので使い方の備忘録。

以下のように、new Array(9)とすると、空の値が9つ入った配列が作成される。

  const arr = [1,2,3,4,4,5,6,6,7];
  const a = new Array(9);

それにfill()メソッドで数字を入れることができる。

  a.fill(1);
  console.log(a);//出力結果→ (9) [1, 1, 1, 1, 1, 1, 1, 1, 1]

第一引数に入れたい数字、第二引数に開始位置、第三引数に終了位置をかく更新方法もある。

  a.fill(2, 3, 6);
  console.log(a);//出力結果→ (9) [1, 1, 1, 2, 2, 2, 1, 1, 1]  

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