LoginSignup
3
1

More than 5 years have passed since last update.

配列で padEnd したい

Last updated at Posted at 2018-01-15

やりたいこと

配列を任意の長さまで任意の文字列で延長したい。具体的には

// 配列を長さが 5 になるまで "*" で埋める。
padEnd([1, 2, 3], 5, '*') //=> [1, 2, 3, "*", "*"]

のような関数がほしい。

実装

const padEnd = (array, length, padElement = null) =>
  Object.assign(new Array(length).fill(padElement), array);

padEnd([1, 2, 3], 5, '*') //=> [1, 2, 3, "*", "*"]
padEnd([1, 2, 3], 5) //=> [1, 2, 3, null, null]

参考

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