initializeArrayWithValues
Initializes and fills an array with the specified values.
- Use
Array.from()to create an array of the desired length,Array.prototype.fill()to fill it with the desired values. - Omit the last argument,
val, to use a default value of0.
const initializeArrayWithValues = (n, val = 0) =>
Array.from({ length: n }).fill(val);initializeArrayWithValues(5, 2); // [2, 2, 2, 2, 2]Example
MDN
Last updated on January 12, 2023