Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Last active September 23, 2020 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telekosmos/bef579a0494b808d483e487875004e55 to your computer and use it in GitHub Desktop.
Save telekosmos/bef579a0494b808d483e487875004e55 to your computer and use it in GitHub Desktop.
Initialize an array in javascript
const initArray = (n, v) => Array(n).fill(v);
const initList = (size, fromZero = true) => Array.from({ length: size }, (_, i) => fromZero? i: i+1)
const l = initArray(5, 0);
// l = [0, 0, 0, 0, 0]
const l2 = initList(5);
// l2 = [0, 1, 2, 3, 4]
const l3 = initList(5, false);
// l3 = [1, 2, 3, 4, 5]
@telekosmos
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment