Skip to content

Instantly share code, notes, and snippets.

@padcom
Created March 16, 2022 23:34
Show Gist options
  • Save padcom/879c0bef7d610ce7549e6e312136eee5 to your computer and use it in GitHub Desktop.
Save padcom/879c0bef7d610ce7549e6e312136eee5 to your computer and use it in GitHub Desktop.
Utility function to create a new array with elements being initialized by calling an initializer
/**
* Create an array by calling an initializer on each element
*
* @param length {Number} length of the created array
* @param init {Function} callback to call on each element of the array
*/
function array(length, init = index => 0) {
return Array.apply(null, { length }).map(Function.call, init)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment