Reorganize snippets

This commit is contained in:
Angelos Chalaris
2023-05-03 21:19:02 +03:00
parent 7511813169
commit 5c913d20bd
1240 changed files with 992 additions and 0 deletions

View File

@ -0,0 +1,22 @@
---
title: Initialize array with values
type: snippet
language: javascript
tags: [array]
cover: flower-portrait-1
dateModified: 2020-10-20T23:02:01+03:00
---
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 of `0`.
```js
const initializeArrayWithValues = (n, val = 0) =>
Array.from({ length: n }).fill(val);
```
```js
initializeArrayWithValues(5, 2); // [2, 2, 2, 2, 2]
```