Files
30-seconds-of-code/snippets/initialize-array-with-range.md
Angelos Chalaris 43f8529cb9 Added samples
2017-12-12 17:50:08 +02:00

11 lines
369 B
Markdown

### Initialize array with range
Use `Array(end-start)` to create an array of the desired length, `map()` to fill with the desired values in a range.
You can omit `start` to use a default value of `0`.
```js
const initializeArrayRange = (end, start = 0) =>
Array.apply(null, Array(end-start)).map( (v,i) => i + start );
// initializeArrayRange(5) -> [0,1,2,3,4]
```