Created
March 16, 2022 23:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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