Initializes and fills an array with the specified values.
Array.from()
to create an array of the desired length, Array.prototype.fill()
to fill it with the desired values.val
, to use a default value of 0
.const initializeArrayWithValues = (n, val = 0) =>
Array.from({ length: n }).fill(val);
initializeArrayWithValues(5, 2); // [2, 2, 2, 2, 2]
JavaScript, Array
Mutates the original array to filter out the values specified, based on a given iterator function.
JavaScript, Array
Converts an array of objects to a comma-separated values (CSV) string that contains only the columns
specified.
JavaScript, Array
Filters an array of objects based on a condition while also filtering out unspecified keys.